You have a database full of customer orders and product data. Someone asks you to pull a list of all active products. Without SQL, you are stuck. With SQL, you write three words and the answer is right there. This lesson shows you exactly how that works.
The Shape of a SQL Query
Every query you write starts with two things: SELECT and FROM.
SELECT tells the database which columns you want back. FROM tells it which table to look in.
SELECT product_name, price, brand
FROM products;
This returns the product_name, price, and brand columns for every row in the products table. Nothing more, nothing less.
SELECT as "what do I want" and FROM as "where do I find it." Every query you ever write starts with this pair.Choosing Your Columns
You can list as many columns as you need, separated by commas. The order you list them is the order they appear in your results.
SELECT first_name, last_name, email, city
FROM customers;
If you want every column without typing them all out, use *:
SELECT *
FROM customers;
Use * when exploring a table for the first time. Avoid it in production queries because it pulls data you probably do not need and slows things down on large tables.
Filtering What Comes Back
A query without a filter returns every row. That is rarely what you want. Add WHERE to narrow things down.
SELECT product_name, price, stock_qty
FROM products
WHERE is_active = 1;
This returns only rows where is_active equals 1, meaning the product is currently live. Everything else is filtered out.
You can also filter on text values:
SELECT first_name, last_name, email
FROM customers
WHERE city = 'Austin';
Text values go inside single quotes. Numbers do not.
Common Mistakes
-- wrong
SELECT product_name price brand
FROM products;
-- correct
SELECT product_name, price, brand
FROM products;
Column names in SELECT must be separated by commas. Missing a comma causes a syntax error.
-- wrong
SELECT first_name, last_name
FROM customer;
-- correct
SELECT first_name, last_name
FROM customers;
Table names must match exactly. customer does not exist; customers does. One wrong character and the query fails.
-- wrong
SELECT first_name, last_name
FROM customers
WHERE city = Austin;
-- correct
SELECT first_name, last_name
FROM customers
WHERE city = 'Austin';
Text values in WHERE must be wrapped in single quotes. Without them, the database thinks Austin is a column name and throws an error.
Quick Recap
- Every query needs
SELECT(which columns) andFROM(which table). - List column names separated by commas, or use
*to get all columns. - Add
WHEREto filter rows. Text values need single quotes; numbers do not. - Table and column names must match the database exactly, including spelling.
product_name, price, and brand columns from the products table for every row.SELECT with three column names and FROM products. No filter needed.SELECT product_name, price, brand
FROM products;
first_name, last_name, and email columns from the customers table, but only for customers whose country is 'Canada'.WHERE clause after FROM customers. Remember to wrap the text value in single quotes.SELECT first_name, last_name, email
FROM customers
WHERE country = 'Canada';
first_name and last_name from the customers table and the total_amount from the orders table. Only include rows where total_amount is greater than 200. Join the two tables on customer_id.JOIN orders o ON c.customer_id = o.customer_id after FROM customers c. Add your WHERE filter after the join.SELECT c.first_name, c.last_name, o.total_amount
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.total_amount > 200;
The simplest query is `SELECT` followed by column names and `FROM` followed by a table name. That is all you need to get data back.
Write your query in a SQL editor or tool, then press run or execute. The database reads your query and returns matching rows instantly.
The `*` is a wildcard that means "all columns." `SELECT *` returns every column in the table without you having to name them individually.
No, SQL keywords are not case-sensitive. Writing `select` works the same as `SELECT`. Most people capitalize keywords to make queries easier to read.