SQL Select
SELECT is the part of a query that names the columns to return. The FROM part names the table those columns come from.
One column
This query names one column. The answer holds that column for every row of the table.
SELECT name FROM products;Two columns
The list can name two columns. The answer shows the name first and the price second, because the list names them in that order.
SELECT name, price FROM products;The order follows the list
Swapping the list swaps the answer. The price now comes first and the name second.
SELECT price, name FROM products;Every column
The star stands for every column. The answer holds all five columns of the products table for one row.
SELECT * FROM products WHERE product_id = 3;Example
SELECT name, category, price FROM products WHERE product_id = 7;Key points
- SELECT: the keyword that names the columns a query returns
- FROM: the keyword that names the table a query reads
- A comma separates the column names in the list.
- The columns come back in the order the list names them.
- SELECT * returns every column of the table.
- A query names its columns, so the answer holds only what the question asked for.
In this example, the SELECT part names three columns of the products table. The WHERE part picks the row with product_id 7. The answer holds the name, category and price of that one product.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท DB Fiddle
Exercise
What does SELECT * return?