SQL Cheat Sheet: 40 queries with examples, free printable PDF
Tips & Tricks

SQL Cheat Sheet: 40 Queries With Examples

Written by FutureSavvy Team
Updated 9 Oct 2026 · 1 min read

Every query below runs against the same four tables: Orders (OrderID, CustomerID, OrderDate, ShipDate, Amount, Region, Status), Customers (CustomerID, CustomerName, City), OrderLines (OrderID, ProductID, Qty, UnitPrice) and Products (ProductID, ProductName, Category). The syntax is SQL Server's, which is what our courses teach; the handful of functions that differ elsewhere are marked. Each one has a one-line note on what it does and the mistake people most often make with it.

Download the printable PDF

Selecting and filtering

SQL
SELECT OrderID, OrderDate, Amount
FROM Orders;
SQL
SELECT TOP 10 OrderID, Amount
FROM Orders
ORDER BY Amount DESC;
SQL
SELECT DISTINCT Region
FROM Orders;
SQL
WHERE Region = 'North' AND Amount > 1000
SQL
WHERE Region IN ('North', 'South', 'East')
SQL
WHERE OrderDate >= '2026-01-01' AND OrderDate < '2026-04-01'
SQL
WHERE CustomerName LIKE 'A%'
SQL
WHERE Status IS NULL
SQL
ORDER BY Region, OrderDate DESC

Aggregating

SQL
SELECT COUNT(*) FROM Orders;
SQL
SELECT Region, SUM(Amount) AS Total, AVG(Amount) AS Average
FROM Orders
GROUP BY Region;
SQL
SELECT Region, SUM(Amount) AS Total
FROM Orders
GROUP BY Region
HAVING SUM(Amount) > 50000;
SQL
SELECT COUNT(DISTINCT CustomerID) AS Customers
FROM Orders;
SQL
SELECT YEAR(OrderDate) AS Yr, MONTH(OrderDate) AS Mth, SUM(Amount) AS Total
FROM Orders
GROUP BY YEAR(OrderDate), MONTH(OrderDate)
ORDER BY Yr, Mth;

Joins

SQL
SELECT o.OrderID, c.CustomerName
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID;
SQL
SELECT o.OrderID, c.CustomerName
FROM Orders o
LEFT JOIN Customers c ON o.CustomerID = c.CustomerID;
SQL
SELECT c.CustomerID, c.CustomerName
FROM Customers c
LEFT JOIN Orders o ON o.CustomerID = c.CustomerID
WHERE o.OrderID IS NULL;
SQL
SELECT p.Category, SUM(l.Qty * l.UnitPrice) AS Revenue
FROM Orders o
JOIN OrderLines l ON l.OrderID = o.OrderID
JOIN Products p ON p.ProductID = l.ProductID
GROUP BY p.Category;

Subqueries and CTEs

SQL
WHERE CustomerID IN (SELECT CustomerID FROM Customers WHERE City = 'Leeds')
SQL
SELECT c.CustomerName
FROM Customers c
WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.CustomerID = c.CustomerID AND o.Amount > 5000);
SQL
WHERE Amount > (SELECT AVG(Amount) FROM Orders)
SQL
WITH Monthly AS (
  SELECT YEAR(OrderDate) AS Yr, MONTH(OrderDate) AS Mth, SUM(Amount) AS Total
  FROM Orders GROUP BY YEAR(OrderDate), MONTH(OrderDate)
)
SELECT * FROM Monthly WHERE Total > 10000;

Conditions, text and dates

SQL
CASE WHEN Amount >= 1000 THEN 'Large'
     WHEN Amount >= 100 THEN 'Medium'
     ELSE 'Small' END AS Band
SQL
SUM(CASE WHEN Status = 'Cancelled' THEN 1 ELSE 0 END) AS Cancelled
SQL
COALESCE(Region, City, 'Unknown')
SQL
CAST(Amount AS DECIMAL(10, 2)) AS Amount2dp
SQL
DATEDIFF(DAY, OrderDate, ShipDate) AS DaysToShip
SQL
WHERE OrderDate >= DATEADD(MONTH, -3, CAST(GETDATE() AS DATE))
SQL
CONCAT(c.CustomerName, ' (', c.City, ')') AS Label
SQL
LEN(TRIM(CustomerName)) AS NameLength
SQL
EOMONTH(OrderDate) AS MonthEnd

Window functions

SQL
ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY OrderDate DESC) AS rn
SQL
SUM(Amount) OVER (PARTITION BY Region ORDER BY OrderDate) AS RunningTotal
SQL
LAG(Amount) OVER (PARTITION BY CustomerID ORDER BY OrderDate) AS PreviousAmount
SQL
RANK() OVER (ORDER BY Amount DESC) AS AmountRank

Combining and changing data

SQL
SELECT CustomerID, OrderDate FROM Orders
UNION ALL
SELECT CustomerID, OrderDate FROM ArchivedOrders;
SQL
UPDATE Orders
SET Status = 'Closed'
WHERE OrderID = 5012;
SQL
DELETE FROM Orders
WHERE Status = 'Test';
SQL
BEGIN TRAN;
UPDATE Orders SET Status = 'Closed' WHERE Region = 'North';
-- check the row count, then one of:
ROLLBACK;  -- or COMMIT;
SQL
CREATE VIEW vMonthlySales AS
SELECT YEAR(OrderDate) AS Yr, MONTH(OrderDate) AS Mth, SUM(Amount) AS Total
FROM Orders GROUP BY YEAR(OrderDate), MONTH(OrderDate);

Which course covers these

Selecting, filtering, GROUP BY and the inner and left joins are SQL Introduction to Querying. SQL Fundamentals of Querying is the one-day version for people who need to read and write everyday queries fast. Subqueries, CTEs, window functions, UNION, transactions and views are SQL Advanced Querying.

SQL Introduction to QueryingSQL Fundamentals of QueryingSQL Advanced Querying

Next available dates

SQL Introduction to Querying

Live, instructor-led training with a real trainer. Book a seat on a scheduled date below, or get a quote for your team to run it privately.

  • 1 Dec 2026

    Tuesday · Virtual · 2 day

    £825per person + VAT

  • 4 Feb 2027

    Thursday · Virtual · 2 day

    £825per person + VAT

  • 5 Apr 2027

    Monday · Virtual · 2 day

    £825per person + VAT

See 4 more dates and the full outline

Ready to train your team?

Tell us what you need and we'll come back to you with a detailed quote.

  • Tailored contentBuilt around your team's work and skill levels.
  • Flexible schedulingDates and times that suit your team, not ours.
  • Your place or onlineDelivered at your office or virtually.
  • Better value per headOne trainer, one day, your whole team.
  • A dedicated trainerYour team gets all of the trainer's attention.
  • Realistic examplesSample data built around the kind of work your team does.
Get a quote for your team

We reply with available dates and a price for your team. No obligation.

Related Articles

  • Calculate Variance Using Excel
    Tips & Tricks

    Can you Calculate Variance Using Excel?

    In this guide, we explain variance as a measure of how widely data points deviate from the mean and shows why understanding this spread is useful for deeper insight and risk assessment. It walks readers through calculating variance in Excel, distinguishing between the VAR.S function for a sample and VAR.P for an entire population, then demonstrates each with a car-sales case study.

  • excel-lookup-multiple-criteria-team
    Tips & Tricks

    How to Use Excel Lookup with Multiple Criteria

    This blog explains how Excel’s LOOKUP functions—particularly XLOOKUP and VLOOKUP—can retrieve data based on multiple criteria. It walks through a step-by-step example of finding an employee’s sales in a specific region, showing both an XLOOKUP formula and a VLOOKUP alternative that uses a helper column.

  • excel_skills_assessment_questionnaire
    Tips & Tricks

    Excel Skills Self-Assessment Questionnaire

    This free, printable Excel Skills Self-Assessment helps you quickly gauge your level and pick the right next step in your learning path. It contains 15 multiple-choice questions spanning navigation, formulas, lookups, tables, PivotTables, charts, dynamic arrays, Power Query, and more. You’ll score yourself and interpret the result to see whether you’re Beginner, Intermediate, or Advanced - then follow tailored course recommendations based on your score.

Insights for modern teams

Stay ahead with our latest learning trends, tools, and success stories. Enter your email address below to receive updates from us.

Future Savvy
Future Savvy

Registered England and Wales: 11477692 VAT Number: GB 3123317 52All trademarks are owned by their respective owners. Click here for details.

  • Mastercard
  • American Express
  • Visa

Future Savvy is a trading name of Systematix Limited. Registered in England and Wales, company number 11477692. Registered office: Blackwell House, Guildhall Yard, London EC2V 5AE.