Learn the most important MySQL keywords and statements. On this page, you will find an overview of all key SQL expressions, their usage, and practical examples. Learn how to correctly use DISTINCT, WHERE, ORDER BY, and other essential SQL statements.
Displays the first and last name of all customers from the customers table.
SELECT first_name, last_name FROM customers;
Displays the first and last name of all employees who have a salary higher than 2000.
SELECT first_name, last_name FROM employees WHERE salary > 2000;
Displays the last name and salary of the first 5 employees from the employees table.
SELECT last_name, salary FROM employees LIMIT 5;
Inserts a new record into the test_employees table with the first name Janez, last name Novak, and the IT department.
INSERT INTO test_employees (first_name, last_name, department)
VALUES ('Janez', 'Novak', 'IT');
Displays the first name, last name, and department of all employees (including the newly added Janez).
SELECT first_name, last_name, department FROM employees;
Keywords are reserved words in MySQL that define the structure, logic, and execution flow of your query. The table below provides a comprehensive overview of the building blocks you will use to direct, group, and transform data inside the simulator.
| Name | Description | Syntax (HTML) | Example |
|---|---|---|---|
| SELECT | Specifies which columns you want to retrieve from the database. | SELECT column FROM table; |
SELECT first_name, last_name FROM employees; |
| FROM | Specifies the table from which the data will be read. | SELECT * FROM table_name; |
SELECT * FROM merchandise; |
| INTO | Used alongside INSERT to target the specific table for data entry. | INSERT INTO table_name ... |
INSERT INTO customers (first_name, last_name) VALUES ('Ann', 'Smith'); |
| VALUES | Defines the literal values being inserted into a table. | ... VALUES (v1, v2); |
INSERT INTO grades (subject, grade) VALUES ('Mathematics', 95); |
| DISTINCT | Eliminates duplicate rows and returns only unique values. | SELECT DISTINCT column FROM table; |
SELECT DISTINCT department FROM sorting; |
| WHERE | Sets conditions to filter rows prior to any data grouping. | ... WHERE condition; |
SELECT * FROM employees WHERE salary = 2000; |
| ORDER BY | Sorts the result set based on one or more columns. | ... ORDER BY column; |
SELECT * FROM sorting ORDER BY salary DESC; |
| ASC | Sorts data in ascending order (smallest to largest, default behavior). | ... ORDER BY column ASC; |
SELECT * FROM first_table ORDER BY age ASC; |
| DESC | Sorts data in descending order (largest to smallest). | ... ORDER BY column DESC; |
SELECT * FROM merchandise ORDER BY price DESC; |
| GROUP BY | Groups rows sharing identical values into summary rows for analysis. | ... GROUP BY column; |
SELECT department, COUNT(*) FROM employees GROUP BY department; |
| HAVING | Sets filter conditions for groups that are already grouped (used with GROUP BY). | ... GROUP BY col HAVING condition; |
SELECT department FROM employees GROUP BY department HAVING AVG(salary) = 1800; |
| LIMIT | Restricts the maximum number of rows that the query can return. | ... LIMIT number; |
SELECT * FROM news LIMIT 5; |
| OFFSET | Specifies the number of rows to skip before beginning to return records. | ... LIMIT number OFFSET skip; |
SELECT * FROM news LIMIT 5 OFFSET 10; |
| AS | Creates a temporary name (alias) for a column or a table. | SELECT column AS new_name FROM table; |
SELECT stock_quantity AS stock FROM inventory; |
| JOIN | A keyword used to combine data across multiple tables based on a related column. | ... JOIN other_table ... |
SELECT * FROM students JOIN student_grades; |
| ON | Defines the join predicate linking tables using relational key columns. | ... JOIN t2 ON t1.id = t2.t1_id; |
SELECT * FROM sales_on_example JOIN products_on_example ON product_id = id; |
| USING | A simplified JOIN predicate used when both tables share the exact same column name. | ... JOIN t2 USING (shared_column); |
SELECT * FROM students JOIN student_grades USING (student_id); |
| UNION | Combines the results of multiple SELECT statements into a single output (excludes duplicates). | SELECT c FROM t1 UNION SELECT c FROM t2; |
SELECT first_name FROM students UNION SELECT first_name FROM employees; |
| INTERSECT | Returns only the records that appear within the outputs of all SELECT queries. | SELECT c FROM t1 INTERSECT SELECT c FROM t2; |
SELECT first_name FROM customers INTERSECT SELECT first_name FROM employees; |
| EXCEPT | Returns records from the first SELECT query that are not found in the results of the second. | SELECT c FROM t1 EXCEPT SELECT c FROM t2; |
SELECT first_name FROM employees EXCEPT SELECT first_name FROM customers; |
| CASE | Initiates a conditional statement (if-then-else logic) within an SQL query. | CASE WHEN ... THEN ... ELSE ... END |
SELECT first_name, CASE WHEN department = 'Secretary' THEN 'Office' END FROM employees; |
| WHEN | Evaluates a conditional expression inside the CASE statement. | WHEN condition THEN ... |
SELECT name, price,
CASE
WHEN price > 500 THEN 'Premium'
WHEN price > 100 THEN 'Mid-range'
ELSE 'Budget'
END AS category
FROM products; |
| THEN | Defines the value returned if the associated WHEN condition is met. | ... THEN result |
SELECT first_name, CASE WHEN department = 'Sales' THEN 'Field Worker' END FROM sorting; |
| ELSE | Defines a fallback value if none of the WHEN conditions evaluate to true. | ... ELSE default_result |
SELECT first_name, CASE WHEN department = 'Store' THEN 'Seller' ELSE 'Other' END FROM sorting; |
| END | Closes the conditional CASE statement block. | ... END |
SELECT first_name, CASE WHEN department = 'Maintenance' THEN 'Director' END FROM employees; |