SELECT
4 methodsClauses available within a SELECT statement.
SELECT cols FROM tbl [WHERE condition]Reads rows from a table, optionally filtered by a boolean condition.
Parameters
| Name | Type | Description |
|---|---|---|
| cols | column-list | Comma-separated columns or *. |
| tbl | table | Source table (or joined subquery). |
| condition | boolean | Predicate evaluated per row. |
Returns
ResultSet
Example
mysql
SELECT id, name FROM users WHERE active = 1;SELECT ... FROM a JOIN b ON a.id = b.a_idCombines rows from two tables based on a join predicate; INNER, LEFT, RIGHT, FULL supported.
Parameters
| Name | Type | Description |
|---|---|---|
| JOIN | keyword | INNER, LEFT [OUTER], RIGHT [OUTER], CROSS. |
| ON | predicate | Join condition between the two tables. |
Returns
ResultSet
Example
mysql
SELECT u.name, o.total
FROM users u
LEFT JOIN orders o ON o.user_id = u.id;SELECT cols, agg(...) FROM tbl GROUP BY cols [HAVING condition]Groups rows by the listed columns and applies aggregate functions, filtering groups with HAVING.
Parameters
| Name | Type | Description |
|---|---|---|
| agg | function | Aggregate such as COUNT, SUM, AVG, MAX, MIN. |
| cols | column-list | Grouping columns. |
| condition | boolean | Predicate applied to each group (HAVING). |
Returns
ResultSet
Example
mysql
SELECT dept, COUNT(*) AS n, AVG(salary) AS avg_sal
FROM employees
GROUP BY dept
HAVING AVG(salary) > 50000;SELECT ... ORDER BY col [ASC|DESC] [LIMIT n [OFFSET m]]Sorts the result set by the given columns and limits the number of rows returned.
Parameters
| Name | Type | Description |
|---|---|---|
| col | column | Sort key column or expression. |
| LIMIT | number | Maximum rows to return. |
| OFFSET | number | Rows to skip before starting to return. |
Returns
ResultSet
Example
mysql
SELECT * FROM products
ORDER BY price DESC, name ASC
LIMIT 20 OFFSET 40;