TExcelTAD
Home › MySQL › MySQL SELECT

MySQL SELECT

SELECT reads data. It is by far the most used statement, and most of this course is about its clauses. The simplest form lists the columns you want and the table they come from:

sql
SELECT column1, column2, ...
FROM table_name;
mysql · try it yourself
SELECT name, unit_price
FROM products
WHERE category_id = 3;
+--------------------+------------+
| name               | unit_price |
+--------------------+------------+
| Whole Milk 1L      |       1.45 |
| Aged Gruyere 250g  |       7.95 |
| Greek Yogurt 500g  |       3.95 |
| Salted Butter 250g |       2.85 |
+--------------------+------------+

The result is itself a table (a result set): the columns in the order you named them, one row per matching row in the table. (The WHERE line just keeps the output short; it is the subject of the next lesson.)

Selecting every column

* means "all columns, in table order". It is handy for exploring, but in application code list the columns you need: the query then keeps working when someone adds a column, and the server sends less data.

mysql · try it yourself
SELECT * FROM suppliers;
+-------------+--------------------+---------------+-----------+-------------+------------------+
| supplier_id | name               | contact_name  | city      | country     | phone            |
+-------------+--------------------+---------------+-----------+-------------+------------------+
|           1 | Highland Roasters  | Fiona Grant   | Edinburgh | UK          | +44 131 555 0101 |
|           2 | Golden Crust Ltd   | Marco Bellini | Milan     | Italy       | +39 02 555 0102  |
|           3 | Alpine Dairy Co    | Heidi Keller  | Bern      | Switzerland | NULL             |
|           4 | Sunny Fields Farm  | Diego Ramos   | Valencia  | Spain       | +34 96 555 0104  |
|           5 | Crunch & Co        | Priya Nair    | Leeds     | UK          | +44 113 555 0105 |
|           6 | CleanHome Supplies | NULL          | Rotterdam | Netherlands | +31 10 555 0106  |
+-------------+--------------------+---------------+-----------+-------------+------------------+

Expressions and calculated columns

A select list can contain any expression, not only column names. The column header is the expression text unless you give it a name with AS (more in the Aliases lesson):

mysql · try it yourself
SELECT name,
       unit_price,
       unit_price * 1.20 AS price_with_vat,
       units_in_stock * unit_price AS stock_value
FROM products
WHERE category_id = 2;
+------------------+------------+----------------+-------------+
| name             | unit_price | price_with_vat | stock_value |
+------------------+------------+----------------+-------------+
| Sourdough Loaf   |       4.80 |         5.7600 |      120.00 |
| Butter Croissant |       1.60 |         1.9200 |      192.00 |
| Chocolate Cake   |      18.00 |        21.6000 |      144.00 |
| Rye Crackers     |       2.90 |         3.4800 |        0.00 |
+------------------+------------+----------------+-------------+

SELECT even works without a table, which makes it a convenient calculator:

mysql · try it yourself
SELECT 2 + 3 * 4, 10 / 4, CONCAT('My', 'SQL'), UPPER('shop');
+-----------+--------+---------------------+---------------+
| 2 + 3 * 4 | 10 / 4 | CONCAT('My', 'SQL') | UPPER('shop') |
+-----------+--------+---------------------+---------------+
|        14 | 2.5000 | MySQL               | SHOP          |
+-----------+--------+---------------------+---------------+

Note that 10 / 4 gives 2.5000: / always returns a decimal result in MySQL, with four extra digits by default. Use DIV for integer division.

SELECT DISTINCT

DISTINCT removes duplicate rows from the result. Compare the countries of all customers with the distinct list:

mysql · try it yourself
SELECT COUNT(*) AS all_rows, COUNT(DISTINCT country) AS countries FROM customers;
SELECT DISTINCT country
FROM customers
ORDER BY country;
+----------+-----------+
| all_rows | countries |
+----------+-----------+
|       15 |        11 |
+----------+-----------+
+----------+
| country  |
+----------+
| Bulgaria |
| Czechia  |
| Denmark  |
| France   |
| Germany  |
| Ghana    |
| Italy    |
| Japan    |
| Portugal |
| Spain    |
| UK       |
+----------+

DISTINCT applies to the whole row, not to the first column only. With two columns you get each distinct combination: the three sales reps share the same title and manager, so they collapse into one row, while the two managers differ in title and stay separate:

mysql · try it yourself
SELECT DISTINCT title, manager_id
FROM employees;
+-------------------+------------+
| title             | manager_id |
+-------------------+------------+
| CEO               |       NULL |
| Sales Manager     |          1 |
| Warehouse Manager |          1 |
| Sales Rep         |          2 |
| Packer            |          3 |
+-------------------+------------+

Tip: COUNT(DISTINCT column) counts distinct non-NULL values. It is the quickest way to answer "how many different …?".

Test yourself

What does SELECT * FROM products; return?

  1. Only the primary key
  2. All columns of all rows
  3. The number of rows
  4. The table definition

What does SELECT DISTINCT city, country FROM customers remove?

  1. Rows with a duplicate city
  2. Rows with a duplicate country
  3. Rows where both city and country repeat an earlier row
  4. Nothing

What is the result of SELECT 7 / 2; in MySQL?

  1. 3
  2. 3.5000
  3. 4
  4. An error