Skip to content

MySQL SELECT API

MySQL SELECT statement clauses for querying and combining rows from one or more tables.

1 class · 4 methods

SELECT

4 methods

Clauses available within a SELECT statement.

SELECT cols FROM tbl [WHERE condition]

Reads rows from a table, optionally filtered by a boolean condition.

Parameters

NameTypeDescription
colscolumn-listComma-separated columns or *.
tbltableSource table (or joined subquery).
conditionbooleanPredicate 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_id

Combines rows from two tables based on a join predicate; INNER, LEFT, RIGHT, FULL supported.

Parameters

NameTypeDescription
JOINkeywordINNER, LEFT [OUTER], RIGHT [OUTER], CROSS.
ONpredicateJoin 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

NameTypeDescription
aggfunctionAggregate such as COUNT, SUM, AVG, MAX, MIN.
colscolumn-listGrouping columns.
conditionbooleanPredicate 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

NameTypeDescription
colcolumnSort key column or expression.
LIMITnumberMaximum rows to return.
OFFSETnumberRows to skip before starting to return.

Returns

ResultSet

Example

mysql
SELECT * FROM products
ORDER BY price DESC, name ASC
LIMIT 20 OFFSET 40;