Mandla Mathebula's SQL Playground

Assessment Questions & Answers

  1. A list of clients with a CustomerStatusId of 1, ordered by Surname then Firstname. Columns: Surname, Firstname, CustomerStatusId, CreateDateTime.
  2. A list of stock items that were sold in January 2018. Stock Description only.
  3. A list of stock items that were not sold in January 2018. Stock Description only.
  4. Top 10 highest selling stock items for January 2018. Fields: Description, Quantity Sold.
  5. Top 10 customers for January 2018 by sale value. Fields: Firstname, Surname, Number of Sales, Value of Sales.

20 customers, 20 stock items, 20 sales, 35 sale items — all pre-loaded in your browser.

Write SQL

Ctrl + Enter to run

Results

Click "Run" or select a query above to see results

Mandla's SQL Glossary

Reading Data

SELECT
Choose which columns to show. SELECT Firstname, Surname FROM Customer;
FROM
Which table to pull data from.
WHERE
Filter rows by a condition. WHERE CustomerStatusId = 1
ORDER BY
Sort results. ASC = A→Z, DESC = Z→A.
LIMIT
Return only the first N rows. LIMIT 10
DISTINCT
Remove duplicate rows from results.

Modifying Data

INSERT INTO
Add a new row. INSERT INTO Customer VALUES (...);
UPDATE
Change existing data. UPDATE Customer SET Firstname = 'New' WHERE CustomerId = 1;
DELETE
Remove rows. DELETE FROM Customer WHERE CustomerId = 21;

Combining Tables

INNER JOIN
Combine two tables, keeping only rows that match in both.
ON
The condition that links tables. ON Sale.CustomerId = Customer.CustomerId
NOT IN
Exclude rows where a value appears in a subquery.

Summarising Data

GROUP BY
Group rows together and summarise them (like a pivot table).
SUM()
Add up values. SUM(Quantity * Price)
COUNT()
Count how many rows in each group.
AS
Give a column a display name. SUM(Quantity) AS TotalSold

Key Concepts

Table
A collection of data in rows and columns, like a spreadsheet.
Primary Key (PK)
A unique identifier for each row. No duplicates allowed.
Foreign Key (FK)
A column that links to another table's Primary Key.
NULL
Means "no value" — different from 0 or empty string.