SQL Language Reference

SELECT

The SELECT statement retrieves rows from one or more tables.

SELECT column1, column2
FROM dbo.TableName
WHERE condition
ORDER BY column1 ASC;

Common clauses:

  • WHERE – filter rows
  • GROUP BY – aggregate rows
  • HAVING – filter groups
  • ORDER BY – sort result set

INSERT

Insert new rows into a table.

INSERT INTO dbo.Products (ProductName, Price, InStock)
VALUES ('Gadget', 19.99, 1);

Use INSERT ... SELECT to copy from another table.

UPDATE

Modify existing rows.

UPDATE dbo.Employees
SET Salary = Salary * 1.05
WHERE Department = 'Sales';

DELETE

Remove rows from a table.

DELETE FROM dbo.Logs
WHERE LogDate < DATEADD(month, -6, GETDATE());

Use TRUNCATE TABLE for fast removal of all rows.

CREATE TABLE

Define a new table with columns and constraints.

CREATE TABLE dbo.Customers (
    CustomerID INT PRIMARY KEY IDENTITY(1,1),
    FirstName NVARCHAR(50) NOT NULL,
    LastName  NVARCHAR(50) NOT NULL,
    Email     NVARCHAR(255) UNIQUE,
    CreatedAt DATETIME2 DEFAULT SYSDATETIME()
);

ALTER TABLE

Change the structure of an existing table.

ALTER TABLE dbo.Customers
ADD PhoneNumber NVARCHAR(20);
ALTER TABLE dbo.Customers
DROP COLUMN PhoneNumber;

Built‑in Functions

SQL Server provides a large set of functions.

  • String: LEN, SUBSTRING, REPLACE
  • Date & Time: GETDATE, DATEADD, DATEDIFF
  • Aggregate: SUM, AVG, COUNT, MAX, MIN
  • System: SCOPE_IDENTITY, @@IDENTITY
SELECT LEN(FirstName) AS NameLength
FROM dbo.Customers
WHERE DATEDIFF(year, CreatedAt, GETDATE()) > 2;

Data Types

Common data types used in SQL Server.

CategoryTypes
Integertinyint, smallint, int, bigint
Exact Numericdecimal(p,s), numeric(p,s), money, smallmoney
Floating‑pointfloat, real
Date & Timedate, datetime2, datetime, smalldatetime, time, datetimeoffset
Stringchar, varchar, nchar, nvarchar, text, ntext
Binarybinary, varbinary, image
Otherbit, uniqueidentifier, xml, json