SQL course menu
SQL SELECT Statement: How to Read Data From a Table
Learn the SQL SELECT statement step by step: choose columns, select all columns with *, calculate new values, and avoid the most common beginner mistakes.
On this page
SELECT is the statement you will write more than any other in SQL. It reads data from a table and gives it back to you as a result. Whenever you want to look at data, you start with SELECT.
In this lesson you will learn how to pick the columns you want, how to read every column at once, and how to calculate new values on the fly.
The example table#
All examples use this employees table:
SELECT syntax#
SELECT column1, column2, ...
FROM table_name;- After
SELECT, list the columns you want, separated by commas. - After
FROM, write the table that contains them. - The result comes back as a table with only the columns you asked for, in the order you listed them.
Select specific columns#
To see just the name and city of every employee:
SELECT name, city
FROM employees;name | city
------+----------
Asha | Mumbai
Ravi | Bengaluru
Meera | Pune
John | Mumbai
Priya | DelhiEvery row is returned, because we did not filter anything. Only the two columns we asked for appear.
You can list columns in any order. The result follows your order, not the table's:
SELECT city, name
FROM employees;Select all columns with *#
The asterisk * means "every column":
SELECT *
FROM employees;id | name | department | salary | city
---+-------+-------------+--------+----------
1 | Asha | Sales | 52000 | Mumbai
2 | Ravi | Engineering | 78000 | Bengaluru
3 | Meera | Engineering | 81000 | Pune
4 | John | Marketing | 46000 | Mumbai
5 | Priya | Sales | 61000 | DelhiCalculate new values#
SELECT can do more than copy columns. You can do maths and give the result a name with AS:
SELECT name, salary, salary * 12 AS yearly_salary
FROM employees;name | salary | yearly_salary
------+--------+--------------
Asha | 52000 | 624000
Ravi | 78000 | 936000
Meera | 81000 | 972000
John | 46000 | 552000
Priya | 61000 | 732000Here we pretended salary is a monthly figure. salary * 12 is calculated for every row, and AS yearly_salary gives the new column a readable name. That name is called an alias.
The table itself is not changed. SELECT only reads data; the calculation exists only in the result.
Select text and fixed values#
You can also select values that are not in the table at all. This is handy for labels and quick tests:
SELECT name, 'Employee' AS role
FROM employees;name | role
------+---------
Asha | Employee
Ravi | Employee
Meera | Employee
John | Employee
Priya | EmployeeSome databases (MySQL, PostgreSQL, SQLite, SQL Server) even let you SELECT without a table, which is a nice way to try out expressions:
SELECT 2 + 3 AS total;total
-----
5How SELECT works behind the scenes#
It helps to imagine the database doing the work in this order:
FROM: open theemployeestable.SELECT: for every row, build a new row containing only the requested columns and calculations.- Return all those new rows as the result.
When you add WHERE, GROUP BY and ORDER BY in later lessons, they slot into this order too. Understanding it now will make those lessons much easier.
Common mistakes#
Practice#
Practice exercise
Using the employees table, write a query that returns each employee's name, department, and their salary after a 10% raise, in a column called new_salary.
Show hint
A 10% raise means the new salary is the old salary multiplied by 1.10. Use AS to name the calculated column.
Show solution
SELECT name, department, salary * 1.10 AS new_salary
FROM employees;name | department | new_salary
------+-------------+-----------
Asha | Sales | 57200.00
Ravi | Engineering | 85800.00
Meera | Engineering | 89100.00
John | Marketing | 50600.00
Priya | Sales | 67100.00The exact number of decimal places shown depends on your database.
Key takeaway#
Right now every query returns all rows. In the next lesson, WHERE, you will learn how to keep only the rows you need.
Finished this lesson?