This document details my learnings and assignments on SQL using SQL Server. It covers various SQL operations including data retrieval, joining tables, filtering, grouping, aggregate functions, and more — all explained with detailed notes and examples.
SELECT
StatementUsed to retrieve specific columns from a table.
-- Retrieve first and last names from the Employees table
SELECT firstname, lastname FROM HR.Employees;
CASE
StatementUsed for conditional logic directly within queries.
-- Categorize employees based on their birthdate
SELECT firstname, lastname,
CASE
WHEN birthdate > '1970-01-01' THEN 'Bigger than 1970'
WHEN birthdate > '1960-01-01' THEN 'Bigger than 1960'
WHEN birthdate > '1950-01-01' THEN 'Bigger than 1950'
ELSE 'Bigger than 1940'
END AS indep_year_group
FROM HR.Employees;
INNER JOIN
Combines rows from multiple tables based on matching columns.
SELECT productname, shipname, address, empid, orderdate, region, fax, categoryid, requireddate, postalcode
FROM Production.Products
JOIN Sales.OrderDetails ON Production.Products.productid = Sales.OrderDetails.productid
JOIN Sales.Orders ON Sales.OrderDetails.orderid = Sales.Orders.orderid
JOIN Sales.Shippers ON Sales.Shippers.shipperid = Sales.Orders.shipperid
JOIN Sales.Customers ON Sales.Customers.custid = Sales.Orders.custid;
LEFT JOIN
Returns all records from the left table, and matched records from the right.
-- Find products with no orders
SELECT pp.productname
FROM Production.Products pp
LEFT JOIN Sales.OrderDetails so ON so.productid = pp.productid
WHERE so.productid IS NULL;