SQL SELECT Columns
What you will learn
- How to pick some columns and put them in the order you want
- Why
SELECT *is good for looking and risky in programs - How to write a column with its table name (
table.column) - How to add constants to a result, and how
SELECTworks without a table - What the error for a wrong column name says
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. Pick the columns you need
SELECT is followed by a list of columns, separated by commas. The result has exactly those columns, in that order:
SELECT first_name, last_name, city FROM customers LIMIT 5;
first_name last_name city
---------- --------- ------------
Vik Ahmed Leeds
Wanda Ahmed Leeds
Dana Costa Cairo
Noah Nair Porto Alegre
Hugo Rossi Porto Alegre
(5 rows)
SELECT full_name, city, risk_level FROM customers LIMIT 5;
full_name city risk_level
----------- ------- ----------
Chen Park Lima normal
Rafi Uddin Manila normal
Grace Kamau Toronto normal
Mei Bianchi Osaka normal
Farid Lee Austin normal
(5 rows)
SELECT first_name, last_name, enrolled_year FROM students LIMIT 5;
first_name last_name enrolled_year
---------- --------- -------------
Vera Khan 2026
Elena Haque 2024
Chloe Garcia 2025
Quinn Meyer 2023
Dana Torres 2025
(5 rows)
(LIMIT 5 keeps the first five rows; chapter 2 covers it.) The order in the SELECT list is the order of the result columns. It does not have to match the order in the table:
SELECT city, last_name, first_name FROM customers LIMIT 5;
city last_name first_name
------------ --------- ----------
Leeds Ahmed Vik
Leeds Ahmed Wanda
Cairo Costa Dana
Porto Alegre Nair Noah
Porto Alegre Rossi Hugo
(5 rows)
SELECT risk_level, city, full_name FROM customers LIMIT 5;
risk_level city full_name
---------- ------- -----------
normal Lima Chen Park
normal Manila Rafi Uddin
normal Toronto Grace Kamau
normal Osaka Mei Bianchi
normal Austin Farid Lee
(5 rows)
SELECT enrolled_year, last_name, first_name FROM students LIMIT 5;
enrolled_year last_name first_name
------------- --------- ----------
2026 Khan Vera
2024 Haque Elena
2025 Garcia Chloe
2023 Meyer Quinn
2025 Torres Dana
(5 rows)
The same column can even appear twice. The rows are always the same rows; only the columns you ask for change.
2. SELECT * and why to be careful
* means "all columns". It is handy for a first look (lesson 1.1):
SELECT * FROM customers LIMIT 3;
customer_id first_name last_name email phone city country joined_on is_active
----------- ---------- --------- ------------------------- ------------ ----- -------------- ---------- ---------
1 Vik Ahmed vik.ahmed37@example.com +35 49854883 Leeds United Kingdom 2025-10-01 1
2 Wanda Ahmed wanda.ahmed70@example.com NULL Leeds United Kingdom 2024-01-17 1
3 Dana Costa dana.costa5@example.com +58 13864688 Cairo Egypt 2025-10-17 1
(3 rows)
SELECT * FROM customers LIMIT 3;
customer_id full_name birth_date city joined_on risk_level
----------- ----------- ---------- ------- ---------- ----------
1 Chen Park 1956-03-20 Lima 2025-05-07 normal
2 Rafi Uddin 1990-10-10 Manila 2020-01-08 normal
3 Grace Kamau 1961-11-15 Toronto 2025-03-04 normal
(3 rows)
SELECT * FROM students LIMIT 3;
student_id first_name last_name birth_date enrolled_year email guardian_name
---------- ---------- --------- ---------- ------------- ------------------------------ -------------
1 Vera Khan 2012-04-28 2026 vera.khan38@student.example Amir Khan
2 Elena Haque 2012-08-30 2024 elena.haque48@student.example Nadia Haque
3 Chloe Garcia 2010-10-09 2025 chloe.garcia13@student.example Mina Garcia
(3 rows)
But in a program or a saved report, name the columns instead of using *:
- A table can change. If someone adds a column later,
SELECT *silently returns the extra column, and the program may break or leak data it should not show. - It reads more than needed. Wide tables carry many columns, and each one has to be read and sent.
- It hides intent. A named list tells the next reader what the query is for.
3. Name the table: table.column
A column can be written with its table: table.column. With one table it makes no difference:
SELECT customers.first_name, customers.city FROM customers LIMIT 5;
first_name city
---------- ------------
Vik Leeds
Wanda Leeds
Dana Cairo
Noah Porto Alegre
Hugo Porto Alegre
(5 rows)
SELECT customers.full_name, customers.risk_level FROM customers LIMIT 5;
full_name risk_level
----------- ----------
Chen Park normal
Rafi Uddin normal
Grace Kamau normal
Mei Bianchi normal
Farid Lee normal
(5 rows)
SELECT students.first_name, students.enrolled_year FROM students LIMIT 5;
first_name enrolled_year
---------- -------------
Vera 2026
Elena 2024
Chloe 2025
Quinn 2023
Dana 2025
(5 rows)
It matters when a query uses several tables that have a column with the same name (the chapter on joins). You can also ask for all columns of one table with table.*:
SELECT customers.* FROM customers LIMIT 3;
customer_id first_name last_name email phone city country joined_on is_active
----------- ---------- --------- ------------------------- ------------ ----- -------------- ---------- ---------
1 Vik Ahmed vik.ahmed37@example.com +35 49854883 Leeds United Kingdom 2025-10-01 1
2 Wanda Ahmed wanda.ahmed70@example.com NULL Leeds United Kingdom 2024-01-17 1
3 Dana Costa dana.costa5@example.com +58 13864688 Cairo Egypt 2025-10-17 1
(3 rows)
SELECT customers.* FROM customers LIMIT 3;
customer_id full_name birth_date city joined_on risk_level
----------- ----------- ---------- ------- ---------- ----------
1 Chen Park 1956-03-20 Lima 2025-05-07 normal
2 Rafi Uddin 1990-10-10 Manila 2020-01-08 normal
3 Grace Kamau 1961-11-15 Toronto 2025-03-04 normal
(3 rows)
SELECT students.* FROM students LIMIT 3;
student_id first_name last_name birth_date enrolled_year email guardian_name
---------- ---------- --------- ---------- ------------- ------------------------------ -------------
1 Vera Khan 2012-04-28 2026 vera.khan38@student.example Amir Khan
2 Elena Haque 2012-08-30 2024 elena.haque48@student.example Nadia Haque
3 Chloe Garcia 2010-10-09 2025 chloe.garcia13@student.example Mina Garcia
(3 rows)
4. Constants in a result
Besides columns, a SELECT list can hold constants: fixed text or numbers that are repeated on every row. Text goes in single quotes:
SELECT first_name, 'yes' AS checked, 42 AS answer FROM customers LIMIT 3;
first_name checked answer
---------- ------- ------
Vik yes 42
Wanda yes 42
Dana yes 42
(3 rows)
SELECT full_name, 'yes' AS checked, 42 AS answer FROM customers LIMIT 3;
full_name checked answer
----------- ------- ------
Chen Park yes 42
Rafi Uddin yes 42
Grace Kamau yes 42
(3 rows)
SELECT first_name, 'yes' AS checked, 42 AS answer FROM students LIMIT 3;
first_name checked answer
---------- ------- ------
Vera yes 42
Elena yes 42
Chloe yes 42
(3 rows)
AS checked gives the new column a name (lesson 1.4 covers AS). Single quotes are for text values; do not use double quotes for text.
5. SELECT without a table
FROM is optional when you ask for values that do not come from a table. This is a good way to test an idea quickly:
SELECT 'hello' AS greeting, 2 + 3 AS five;
greeting five
-------- ----
hello 5
(1 row)
SELECT 'hello' AS greeting, 2 + 3 AS five;
greeting five
-------- ----
hello 5
(1 row)
SELECT 'hello' AS greeting, 2 + 3 AS five;
greeting five
-------- ----
hello 5
(1 row)
You get one row. We use this style a lot in later lessons to try functions on small values.
6. When the name is wrong
A misspelt or invented column name gives an error, not an empty result:
SELECT first_name, nme FROM customers LIMIT 3;
OperationalError: no such column: nme
SELECT full_name, nme FROM customers LIMIT 3;
OperationalError: no such column: nme
SELECT first_name, nme FROM students LIMIT 3;
OperationalError: no such column: nme
no such column: nme tells you which name SQLite could not find. Check the spelling against the schema (lesson 1.1), fix it and run again. Reading the error message is the fastest way to find the problem: it names the thing that is wrong.
Worked example: a short list for a report
A colleague wants a short list with three details per row and the most useful column first. You:
- look at the columns with
SELECT * ... LIMIT 3(section 2); - choose the three columns you need;
- write them in the order the reader wants (section 1);
- add a constant column if the report needs a fixed label (section 4).
The query stays correct even if the table later gets more columns, because it names what it needs. The people and values are made up.
Common mistakes
SELECT *in a program. Name the columns instead.- A trailing comma before
FROM(SELECT a, b, FROM t): a syntax error. - Double quotes for text (
"yes"). Text uses single quotes; double quotes are for names. - A missing comma between two columns: SQL reads the second name as an alias of the first.
Key takeaways
SELECTlists the columns you want, in the order you want.*is fine for looking; name the columns in anything that will be reused.table.columnnames a column unambiguously;table.*means all columns of one table.- Constants (
'text', numbers) can appear in the list, andSELECTcan work without a table. - An error such as
no such columnnames the problem; read it.
Practice
1. Write a query that shows only two columns of a table you know, in reverse order.
Show answer
For example SELECT city, first_name FROM customers LIMIT 5; (any two columns of a real table, in the order you want).
2. Why should a program not use SELECT *?
Show answer
The table can gain columns later, so the result changes by itself; it also reads more than needed and hides what the query is for.
3. What is the difference between 'yes' and "yes"?
Show answer
Single quotes make a text value. Double quotes are for names of columns or tables, so "yes" is read as a column called yes.
4. What does SELECT 7 * 6 AS result; return?
Show answer
One row with one column named result that holds 42. No table is needed.
5. A query fails with no such column: citty. What is wrong and what do you do?
Show answer
The column name is misspelt. Check the real name in the schema (lesson 1.1) and correct it.
6. What does SELECT customers.* FROM customers return?
Show answer
All columns of the customers table, the same as SELECT * here, because only one table is used.