01 / 20

SQL SELECT
Queries

Learn to write SELECT statements that retrieve exactly the columns you need from a database: the most fundamental skill in SQL.

SQL

What Is a Database?

A database is an organized collection of data stored in tables. Think of it like a spreadsheet: each table has rows (records) and columns (fields). SQL is the language we use to talk to databases and ask them questions.

id name grade email 1 Alice 10 alice@... 2 Bob 11 bob@... 3 Charlie 10 charlie@... 4 Diana 12 diana@... ← Columns (fields) → Rows →

What Does SELECT Do?

SELECT is the SQL keyword that retrieves data from a database. It lets you choose which columns to see and which table to pull them from. Every data question you ask starts with SELECT.

SELECT
Specifies which columns you want to retrieve
FROM
Specifies which table to pull the data from

Why SELECT Matters

Real databases contain millions of rows and dozens of columns. Retrieving only the columns you need makes queries faster, results clearer, and your work more professional.

Performance
Fetching fewer columns means the database does less work and returns data faster
Clarity
Seeing only relevant columns helps you focus on what matters
Security
Avoiding unnecessary columns prevents accidental exposure of sensitive data

The SELECT Syntax

SELECT column1, column2, ... FROM table_name;
1
SELECT: the keyword that starts every query
2
Column list: one or more column names, separated by commas
3
FROM: specifies the table you are querying
4
Semicolon: ends the SQL statement (required in most systems)

SELECT * vs Specific Columns

The asterisk * is a wildcard meaning "all columns." It is useful for quick exploration but should be avoided in production queries.

SELECT *
Returns every column in the table. Quick but wasteful on large tables.
SELECT col1, col2
Returns only the columns you name. Faster, cleaner, and more intentional.

Keywords vs Identifiers

SQL has two kinds of words in every query. Understanding the difference prevents errors and confusion.

Keywords Identifiers
Definition Reserved words built into SQL Names you (or the designer) create
Examples SELECT, FROM, WHERE students, name, grade
Case Conventionally UPPERCASE Conventionally lowercase
Can you rename? No Yes (with aliases)

How to Write a SELECT Query

1
Identify the table. What table holds the data you need? Write: FROM table_name
2
Pick your columns. Which fields answer your question? List them after SELECT, separated by commas.
3
Combine. Put SELECT + columns + FROM + table together.
4
End with a semicolon. The ; tells the database: "This statement is complete."
5
Run and verify. Execute the query. Check that the output has the right columns and data.

Example 1: One Column

Question: Get the names of all students.

SELECT name FROM students;
id name grade 1 Alice 10 2 Bob 11 3 Charlie 10 4 Diana 12 name Alice Bob Charlie Diana students table result

Example 2: Multiple Columns

Question: Get the name and grade of every student.

SELECT name, grade FROM students;
namegrade
Alice10
Bob11
Charlie10
Diana12

Example 3: SELECT * in Action

Question: See every column and every row of the students table.

SELECT * FROM students;
idnamegradeemail
1Alice10alice@school.edu
2Bob11bob@school.edu
3Charlie10charlie@school.edu
4Diana12diana@school.edu

Column Order Matters

The order you list columns after SELECT controls the order they appear in the result. This is useful for presenting data clearly.

-- grade first, then name SELECT grade, name FROM students;
gradename
10Alice
11Bob
10Charlie
12Diana

Renaming with AS

You can give a column a temporary display name using the AS keyword. This is called an alias. It does not change the actual table: it only changes how the result looks.

SELECT name AS student_name, grade AS year_level FROM students;
student_nameyear_level
Alice10
Bob11
Charlie10
Diana12

Removing Duplicates with DISTINCT

When a column has repeated values, SELECT DISTINCT returns only unique entries.

SELECT DISTINCT grade FROM students;
Without DISTINCT
10, 11, 10, 12 (four rows, grade 10 appears twice)
With DISTINCT
10, 11, 12 (three rows, each grade appears once)

Common Mistakes

✕ Missing comma
Writing SELECT name grade without a comma between columns causes a syntax error.
✕ Forgetting FROM
Writing SELECT name; without FROM tells the database nothing about which table to search.
✕ Misspelled column
Writing SELECT naem instead of name triggers an "unknown column" error. Always double-check column names.
✕ Using SELECT * everywhere
Lazy habit: fetching all columns when you only need one or two wastes resources and hides your intent.

SELECT Query Cheat Sheet

Pattern Syntax Use When
All columns SELECT * FROM t; Quick exploration
Specific columns SELECT a, b FROM t; You know what you need
Renamed columns SELECT a AS x FROM t; Clearer output labels
Unique values SELECT DISTINCT a FROM t; Remove duplicate rows

Question 1 of 5

Which SQL keyword is used to retrieve data from a table?

Question 2 of 5

What does SELECT * return?

Question 3 of 5

Given a table called books, which query retrieves only the title and author columns?

Question 4 of 5

What does the AS keyword do in a SELECT query?

Question 5 of 5

If the employees table has a department column with values: Sales, Engineering, Sales, HR, Engineering, Sales: how many rows does this query return?

SELECT DISTINCT department FROM employees;