Tables are just spreadsheets with attitude
A database is a collection of tables. Each table has columns (like name, price) and rows (one pizza per row). Pixel Pizzeria has four tables: pizzas, customers, orders and chefs. Peek at them in the Schema panel on the right any time.
The SELECT statement asks the database to give some of that data back. * means "every column".
Example: Show me everything on the menu
SELECT * FROM pizzas;Reads as: "select all columns from the pizzas table". Hit Run and watch the whole menu appear.
Pick only the columns you want
Grabbing * is fine for poking around, but real queries name their columns. It is faster and it makes the result easier to read. Separate column names with commas.
Example: Just names and prices
SELECT name, price FROM pizzas;Columns come back in the order you list them, not the order they live in the table.
Rename things with AS
An alias gives a column a temporary nickname in the output. Handy when you compute something, or when the real column name is ugly. AS is optional in most databases but makes your intent obvious.
Example: Friendly column names
SELECT name AS pizza, price AS cost_in_dollars FROM pizzas;The table itself is untouched. Aliases only change how the result looks.
Do math right in the SELECT
You can write expressions using + - * / on columns. This is where SQL starts feeling like a calculator that happens to know your data.
Example: Prices with 20% tip baked in
SELECT name, price, price * 1.2 AS price_with_tip FROM pizzas;The price column stays put; the third column is computed row by row.
DISTINCT: no duplicates please
If you select a column with repeated values, you get repeats. DISTINCT collapses them into one row per unique value.
Example: Which categories exist?
SELECT DISTINCT category FROM pizzas;Try removing DISTINCT and run it again. You will see "Classic" four times.
Practice exercises
Meet the chefs
Show every column from the
chefstable.Customer cities
Show the
nameandcityof every customer (in that column order).Calorie per dollar
For every pizza show its
nameand a new column calledcal_per_dollarwhich iscaloriesdivided byprice.Where do customers live?
List each unique
cityfrom thecustomerstable, with no duplicates.
Open this page in a browser to run your SQL and get instant, auto-graded feedback.
Boss battle
Take the SELECT quiz: 5 timed questions.