SQL Command Practice
SQL asks a relational engine for rows, not a shell for flags. This track writes SELECT, JOIN, WHERE, GROUP BY, and aggregates against a small company schema and checks the result set, not a multiple-choice guess about syntax.
Command Reference
where
| Command | Description |
|---|---|
SELECT name FROM employees WHERE department_id = 1 | List employees in the Engineering department (department_id 1). |
SELECT name FROM employees WHERE salary > 80000 | List employees earning more than 80,000. |
SELECT id, amount FROM orders WHERE status = 'completed' | List the id and amount of all completed orders. |
SELECT name FROM employees WHERE hire_date > '2020-12-31' | List employees hired after 2020. |
SELECT id, amount FROM orders WHERE status = 'pending' AND amount > 1000 | List pending orders with an amount greater than 1,000. |
select
| Command | Description |
|---|---|
SELECT name FROM employees | List the names of all employees. |
SELECT amount FROM orders | List the amount of every order. |
SELECT name, salary FROM employees | Show each employee's name and salary. |
SELECT name FROM departments | List the names of all departments. |
SELECT DISTINCT status FROM orders ORDER BY status | List each distinct order status in alphabetical order. |
aggregate
| Command | Description |
|---|---|
SELECT COUNT(id) FROM employees | Count the total number of employees. |
SELECT SUM(amount) FROM orders | Calculate the total amount across all orders. |
SELECT AVG(salary) FROM employees | Calculate the average employee salary. |
SELECT MAX(salary) FROM employees | Find the highest employee salary. |
SELECT MIN(amount) FROM orders WHERE status = 'completed' | Find the smallest amount among completed orders. |
order
| Command | Description |
|---|---|
SELECT name, salary FROM employees ORDER BY salary DESC | List employees sorted by salary from highest to lowest. |
SELECT name FROM employees ORDER BY name | List employee names in alphabetical order. |
SELECT amount FROM orders ORDER BY amount ASC | List order amounts from lowest to highest. |
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3 | List the three highest-paid employees. |
SELECT name, budget FROM departments ORDER BY budget DESC | List departments sorted by budget from highest to lowest. |
join
| Command | Description |
|---|---|
SELECT e.name, d.name FROM employees e JOIN departments d ON e.department_id = d.id | List each employee's name alongside their department name. |
SELECT o.id, e.name FROM orders o JOIN employees e ON o.employee_id = e.id | List each order id with the name of the employee who placed it. |
SELECT e.name FROM employees e JOIN departments d ON e.department_id = d.id WHERE d.name = 'Engineering' | List the names of all employees in the Engineering department. |
SELECT e.name, d.name FROM employees e LEFT JOIN departments d ON e.department_id = d.id ORDER BY e.id | List every employee with their department name, ordered by employee id. |
SELECT e.name, o.amount, o.status FROM orders o JOIN employees e ON o.employee_id = e.id | List each order's employee name, amount, and status. |
group
| Command | Description |
|---|---|
SELECT department_id, COUNT(id) FROM employees GROUP BY department_id ORDER BY department_id | Count employees in each department, ordered by department id. |
SELECT employee_id, SUM(amount) FROM orders GROUP BY employee_id ORDER BY employee_id | Calculate total order amount per employee, ordered by employee id. |
SELECT status, COUNT(id) FROM orders GROUP BY status ORDER BY status | Count orders grouped by status, ordered alphabetically by status. |
SELECT d.name, COUNT(e.id) FROM departments d JOIN employees e ON d.id = e.department_id GROUP BY d.name ORDER BY d.name | Count employees per department name, ordered alphabetically by department. |
SELECT department_id, AVG(salary) FROM employees GROUP BY department_id ORDER BY department_id | Calculate average salary per department, ordered by department id. |
Key Use Cases
- Filter employees with WHERE and compare null-safe predicates
- Join employees to departments without duplicating rows by accident
- Sort and limit result sets for a leaderboard-style query
- GROUP BY with HAVING to keep only groups that meet a threshold
- Compute COUNT, SUM, and AVG over orders in the sample schema
Frequently Asked Questions
Why does this track check query results instead of exact SQL text?
Equivalent SQL can be written many ways. The sandbox compares the result set so JOIN order and alias names are not a trap, while a wrong filter still fails.
What is the difference between WHERE and HAVING?
WHERE filters rows before grouping. HAVING filters groups after aggregation. Putting an aggregate in WHERE is invalid; this track tests that split.
INNER JOIN vs LEFT JOIN on this schema?
INNER JOIN drops employees without a matching department. LEFT JOIN keeps them with null department columns. Pick the join that matches the question's required rows.
Is this MySQL or PostgreSQL?
The prompts use portable SELECT/JOIN/GROUP BY on a tiny company database. Vendor-only functions are out of scope so the same query idea transfers.
Ready to master SQL commands?
Test your muscle memory with our spaced-repetition quiz system. Free forever.
