Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

Creating a Database

In SQL Server, the CREATE DATABASE statement is used to create a new database. A database provides a container where tables and other database objects can be stored.

What is CREATE DATABASE?

The CREATE DATABASE statement creates a new database in SQL Server.

Once a database is created, we can create tables and other database objects inside it.

SQL
CREATE DATABASE CollegeDB;

Database Creation Flow

The basic process of creating a database can be understood as follows:

SQL Server

                                                             SQL Server
                                                                 ↓
                                                             CREATE DATABASE
                                                                 ↓
                                                             CollegeDB
                                                                 ↓
                                                            Create Tables
                                                                 ↓
                                                             Insert Data

Create a Simple Database

The following statement creates a database named StudentDB.

SQL
CREATE DATABASE StudentDB;

After executing the statement, SQL Server creates the StudentDB database.

Verify the Database

We can check whether the database exists by using the following query:

SQL
SELECT name
FROM sys.databases;

This query displays the databases available on the SQL Server instance.

Output

After executing the query, the database name can be seen in the result.

Database Name
master
model
msdb
tempdb
StudentDB

The exact list of databases depends on the SQL Server installation and databases available on the server.

Select a Database Using USE

After creating a database, the USE statement can be used to select that database for subsequent SQL operations.

SQL
USE StudentDB;

After executing this statement, subsequent table creation statements will be executed in the selected database.

Create Database and Table

We can create a database first and then create a table inside that database.

SQL
CREATE DATABASE StudentDB;

USE StudentDB;

CREATE TABLE Students
(
    StudentId INT,
    StudentName VARCHAR(100),
    Age INT,
    Course VARCHAR(50)
);

What Can a Database Contain?

A SQL Server database can contain different types of database objects.

Tables

Store data in rows and columns.

Views

Provide a way to query data through a saved query definition.

Stored Procedures

Store reusable SQL statements and logic.

Functions

Perform reusable operations and return values.

Indexes

Help SQL Server locate data efficiently.

Constraints

Help enforce rules on table data.

Check Before Creating a Database

Before creating a database, we can check whether a database with the same name already exists.

SQL
IF DB_ID('TrainingDB') IS NULL
BEGIN
    CREATE DATABASE TrainingDB;
END;

This prevents the CREATE DATABASE statement from being executed when the specified database already exists.

Database Naming Guidelines

  • Choose a meaningful database name.
  • Keep naming consistent across the project.
  • Avoid unnecessarily complicated names.
  • Use names that clearly describe the application's data.
Example: StudentManagementDB is more descriptive than a name such as DB1.
Important

A database should be created with a meaningful name and should be organized properly before creating the application's tables and other database objects.

Summary

In this section, we learned how to create a database using CREATE DATABASE, verify databases using sys.databases, select a database using USE and create tables inside the selected database.