bhylu.com

Advertisements

SQL Quiz

Use this short quiz to check your reasoning rather than how many keywords you remember. Predict each result before running a query. If a prediction is wrong, identify the row or rule that explains the difference.

Understand the idea

Question 1: how many rows does COUNT(email) count among four customers? Question 2: does WHERE city = NULL find Noor? Question 3: how many result rows does a customer-to-order left join have before grouping? Question 4: why can a notebook order produce two copies of an order total after joining all its lines? Write an answer to each before opening the worked checks.

Where to try it

Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.

SQLite sample data · MySQL / MariaDB sample data · See the sample tables

Your task

Write your four answers first. Then change COUNT(email) to COUNT(*) and explain exactly why the result changes.

Show the worked solution
SQLite

SELECT COUNT(email) AS emails FROM customers;
SELECT name FROM customers WHERE city IS NULL;
SELECT COUNT(*) AS joined_rows
FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.customer_id;

Check your result

The answers checked by this script are 3 emails, Noor, and 5 joined rows. For question 4, order 101 has two line rows, so its header amount repeats across those joined rows. Summing it again would double that order's contribution.