Appearance
Basic Queries
SELECT
sql
SELECT * FROM table;
SELECT c1, c2 FROM table;
SELECT c1 AS alias FROM table;
SELECT * FROM table LIMIT 1;DISTINCT
sql
SELECT DISTINCT name FROM grades;WHERE
Adding a WHERE clause restricts query results. Only rows that match the condition are returned.
sql
SELECT * FROM table WHERE condition;sql
SELECT grade
FROM grades
WHERE courseid = 123 AND name = 'Teppo';Returns grades for rows where:
courseidis 123nameis 'Teppo'
Comparison operators
=equal<less than>greater than<=less than or equal>=greater than or equal
Reference: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html
Logical operators
Logical operators combine multiple conditions:
ANDor&&True if both conditions are trueORor||True if either condition is trueNOTor!Negates the conditionXORTrue if exactly one condition is true
Reference: https://dev.mysql.com/doc/refman/5.7/en/logical-operators.html
LIKE
simple string matching
sql
SELECT * FROM names WHERE firstname LIKE '%eppo';IN
Value exists in a list.
sql
SELECT * FROM grades WHERE grade IN (1, 3, 5);This will show the grades where the grade is 1, 3 or 5.
BETWEEN
Filters rows where a value falls within a given range. The range is inclusive — both the start and end values are included.
sql
SELECT * FROM grades WHERE grade BETWEEN 1 AND 3;This returns rows where grade is 1, 2, or 3.
BETWEEN also works with dates and text:
sql
SELECT * FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';IS NULL / IS NOT NULL
Checks whether a value is NULL (missing/unknown). You cannot use = to compare with NULL — you must use IS NULL or IS NOT NULL.
sql
SELECT * FROM grades WHERE grade IS NULL;Returns rows where grade has no value.
sql
SELECT * FROM grades WHERE grade IS NOT NULL;Returns rows where grade has a value.
LIMIT / OFFSET
LIMIT restricts how many rows are returned. OFFSET skips a number of rows before starting to return results.
sql
SELECT * FROM grades LIMIT 10;Returns only the first 10 rows.
sql
SELECT * FROM grades LIMIT 10 OFFSET 20;Skips the first 20 rows, then returns the next 10. Useful for pagination — for example, showing page 3 with 10 results per page.
Aggregate functions
Aggregate functions perform a calculation on a set of rows and return a single value. Often used together with GROUP BY.
COUNT()— number of rowsSUM()— total of numeric valuesAVG()— average of numeric valuesMIN()— smallest valueMAX()— largest value
sql
SELECT COUNT(*) FROM grades;Returns the total number of rows in the grades table.
sql
SELECT AVG(grade) FROM grades;Returns the average grade.
sql
SELECT MIN(grade), MAX(grade) FROM grades;Returns the lowest and highest grade.
sql
SELECT name, SUM(profit) FROM sales GROUP BY name;Returns the total profit for each name.
GROUP BY
sql
SELECT year, SUM(profit) FROM sales GROUP BY year;HAVING
sql
SELECT kunta, COUNT(asukas_id)
FROM asukkaat
GROUP BY kunta
HAVING COUNT(asukas_id) > 5000;ORDER BY
Query results can be sorted in ascending or descending order.
Keywords:
ASC— ascending Numbers increase, text follows alphabetical orderDESC— descending Numbers decrease, text follows reverse alphabetical order
Sorting by a single column
sql
SELECT *
FROM grades
ORDER BY grade DESC;Sorts grades from highest to lowest.
Sorting by multiple columns
You can define multiple sorting criteria.
sql
SELECT grade, name
FROM grades
ORDER BY grade DESC, name ASC;Explanation:
- First sorts by
grade(descending) - If grades are equal, sorts by
name(ascending)
TIP
- Sorting happens after filtering (
WHERE). - Sorting affects only the result set, not stored data.
UNION
sql
SELECT city FROM customers
UNION
SELECT city FROM suppliers
ORDER BY city;Subqueries
IN subquery
sql
SELECT * FROM items
WHERE categoryid IN (
SELECT id FROM categories WHERE department = 'Technology'
);Scalar subquery
sql
SELECT * FROM invoices
WHERE total = (SELECT MAX(total) FROM invoices);Exercises
- Write a query that selects all rows from
gradeswheregradeis greater than 3. - Select rows using
INwith at least three values. - Combine conditions using
ANDandOR. - Use
NOTto exclude certain rows. - Write a query that behaves differently with
ANDvsXOR. - Use DISTINCT and ORDER BY in the same query.
- Use
LIKEto find names that start with 'T'. - Select rows where
gradeisBETWEEN2 and 4. - Find all rows where a column value
IS NULL. - Return only the first 5 rows using
LIMIT. - Use
LIMITandOFFSETto get rows 11–20. - Use
COUNTto find the number of rows in a table. - Use
AVG,MIN, andMAXon a numeric column. - Use
SUMwithGROUP BYto get totals per group. - Sort
gradesbygradedescending andnameascending. - Combine
WHEREandORDER BYin the same query. - Filter groups with
HAVINGto show only groups with more than 3 rows. - Combine result sets using
UNION. - Write IN subqueries
- Write scalar subqueries