SQL Tutorial › Querying Data › SELECT

SQL SELECT Statement

BeginnerLesson 7 of 6912 min read7 queries run on MySQL 8.4

SELECT is the SQL statement you will write more than any other. It reads rows from a table and returns only the columns you ask for. In this lesson every query runs on a real MySQL 8.4 server, and you see the table it reads, the result it returns and what happens at each step.

Advertisement
In this lesson
  • Read specific columns, or every column with SELECT *
  • Calculate new columns such as yearly salary
  • Understand the order in which MySQL runs a query
  • Avoid the most common SELECT mistakes

What does SELECT do?

A database stores data in tables: rows and columns, like a spreadsheet. SELECT asks the database a question and gets a result set back, which is also a table. It never changes the stored data, so it is always safe to run.

Every query in this lesson reads the employees table of our sample company database. Here are the columns we use:

employees · 12 rows (some columns)
emp_idfirst_namelast_namejob_titlesalarycity
100MeeraKapoorCEO250000Mumbai
101AshaRaoEngineering Manager125000Bengaluru
102RahulMehtaSenior Developer98000Bengaluru
103PriyaNairQA Engineer72000Pune
104KaranShahDeveloper72000Bengaluru
105VikramSinghSales Manager105000Mumbai
106NehaGuptaSales Executive54000Mumbai
107ArjunDasSales Executive48000Kolkata
108KavyaIyerMarketing Lead88000Pune
109RohanJoshiContent Writer45000Pune
110SnehaKulkarniHR Manager78000Delhi
111AmitVermaIntern25000Bengaluru

SELECT syntax

MySQL
SELECT column1, column2, ...FROM table_name;
  • SELECT lists the columns you want, separated by commas.
  • FROM names the table to read.
  • The semicolon ; ends the statement. MySQL needs it when you send several statements together.

Select specific columns

Show each employee’s first name and job title:

SQL
SELECT first_name, job_titleFROM employees;
ResultMySQL 8.4
first_namejob_title
MeeraCEO
AshaEngineering Manager
RahulSenior Developer
PriyaQA Engineer
KaranDeveloper
VikramSales Manager
NehaSales Executive
ArjunSales Executive
KavyaMarketing Lead
RohanContent Writer
SnehaHR Manager
AmitIntern
12 rows in set
How this query works, step by step
  1. FROM employees runs first: MySQL opens the employees table and takes all 12 rows.
  2. SELECT first_name, job_title keeps only those two columns and drops the rest.
  3. The columns appear in the order you listed them, not the order they have in the table.
  4. There is no WHERE, so every row comes back: 12 rows in set.
Tip

Click Try it, change the column list (for example add salary), and press Run. The query runs in your browser on a copy of the same table.

Advertisement

Select all columns with SELECT *

The asterisk * means “every column, in table order”:

SQL
SELECT * FROM employees;
ResultMySQL 8.4
emp_idfirst_namelast_namedept_idmanager_idjob_titlesalarycommissionhire_datecity
100MeeraKapoorNULLNULLCEO250000NULL2015-01-05Mumbai
101AshaRao1100Engineering Manager125000NULL2018-04-12Bengaluru
102RahulMehta1101Senior Developer98000NULL2019-07-01Bengaluru
103PriyaNair1101QA Engineer72000NULL2021-01-18Pune
104KaranShah1102Developer72000NULL2022-08-29Bengaluru
105VikramSingh2100Sales Manager105000120002017-11-20Mumbai
106NehaGupta2105Sales Executive5400060002022-03-14Mumbai
107ArjunDas2105Sales Executive4800045002023-06-05Kolkata
108KavyaIyer3100Marketing Lead88000NULL2020-09-09Pune
109RohanJoshi3108Content Writer45000NULL2024-01-22Pune
110SnehaKulkarni4100HR Manager78000NULL2019-02-25Delhi
111AmitVerma1101Intern25000NULL2025-06-02Bengaluru
12 rows in set
How this query works, step by step
  1. FROM employees reads all 12 rows.
  2. * expands to all 10 columns: emp_id, first_name, last_name, dept_id, manager_id, job_title, salary, commission, hire_date, city.
  3. Empty values show as NULL. Only the three sales staff have a commission, and the CEO has no dept_id or manager_id.
Watch out

SELECT * is fine for exploring a table, but avoid it in application code and reports. It sends columns you do not need, makes queries slower on wide tables, and breaks code silently when someone adds or reorders a column.

Calculated columns and aliases

A column in the SELECT list can be an expression. MySQL calculates it for every row. Use AS to give it a readable name (an alias):

SQL
SELECT first_name,       salary,       salary * 12 AS yearly_salaryFROM employees;
ResultMySQL 8.4
first_namesalaryyearly_salary
Meera2500003000000
Asha1250001500000
Rahul980001176000
Priya72000864000
Karan72000864000
Vikram1050001260000
Neha54000648000
Arjun48000576000
Kavya880001056000
Rohan45000540000
Sneha78000936000
Amit25000300000
12 rows in set
How this query works, step by step
  1. FROM employees reads all 12 rows.
  2. For each row, MySQL multiplies that row’s salary by 12.
  3. AS yearly_salary names the new column. Without it, the header would be the expression text salary * 12.
  4. The stored salary values do not change: the calculation exists only in the result.

You can also join text with CONCAT():

SQL
SELECT emp_id,       CONCAT(first_name, ' ', last_name) AS full_name,       cityFROM employees;
ResultMySQL 8.4
emp_idfull_namecity
100Meera KapoorMumbai
101Asha RaoBengaluru
102Rahul MehtaBengaluru
103Priya NairPune
104Karan ShahBengaluru
105Vikram SinghMumbai
106Neha GuptaMumbai
107Arjun DasKolkata
108Kavya IyerPune
109Rohan JoshiPune
110Sneha KulkarniDelhi
111Amit VermaBengaluru
12 rows in set
How this query works, step by step
  1. For every row, CONCAT() glues first_name, a space and last_name into one string.
  2. The result column is called full_name because of the alias.
  3. emp_id and city are copied as they are.
Advertisement

SELECT without a table

SELECT can also evaluate expressions without reading any table. That is handy for testing a function before you use it in a real query:

MySQL
SELECT 7 * 6 AS answer, UPPER('sqatools') AS site, CURDATE() IS NOT NULL AS has_date;
ResultMySQL 8.4
answersitehas_date
42SQATOOLS1
1 row in set
How this query works, step by step
  1. There is no FROM, so MySQL evaluates each expression once and returns one row.
  2. 7 * 6 is 42, UPPER() turns the text to capitals, and a comparison returns 1 for true.

The order MySQL runs a query

You write a query in one order but MySQL runs it in another. Knowing this explains most beginner errors, such as “unknown column” when you use an alias in WHERE.

StepClauseWhat happens
1FROM / JOINPick the tables and combine them
2WHEREKeep only the rows that match the condition
3GROUP BYPut rows into groups
4HAVINGKeep only the groups that match
5SELECTCalculate the output columns and aliases
6DISTINCTRemove duplicate rows
7ORDER BYSort the result
8LIMITReturn only the first N rows

Because SELECT (step 5) runs after WHERE (step 2), an alias created in SELECT does not exist yet inside WHERE:

MySQL
SELECT first_name, salary * 12 AS yearly_salaryFROM employeesWHERE yearly_salary > 1000000;
ResultMySQL 8.4
ERROR 1054 (42S22): Unknown column 'yearly_salary' in 'where clause'
Note

Repeat the expression in WHERE (WHERE salary * 12 > 1000000) or use a subquery or CTE. ORDER BY runs after SELECT, so ORDER BY yearly_salary does work.

Common SELECT mistakes

  • Missing comma between columns. SELECT first_name job_title FROM employees does not fail: MySQL reads job_title as an alias for first_name, so you get one column with the wrong header.
  • Comma after the last column. SELECT first_name, FROM employees is a syntax error.
  • Quoting column names with single quotes. SELECT 'salary' FROM employees returns the text salary on every row. Write column names without quotes; wrap a name in backticks only when it has spaces or is a reserved word (see SQL Syntax).
  • Expecting a fixed row order. Without ORDER BY the database may return rows in any order. Never rely on the order you happen to see.

Here is the missing-comma trap in action:

SQL
SELECT first_name job_titleFROM employeesLIMIT 3;
ResultMySQL 8.4
job_title
Meera
Asha
Rahul
3 rows in set
Advertisement

Predict the result

What does this query return? Think about how many columns, and what the header is.

SELECT last_name, salary / 1000 AS salary_kFROM employeesLIMIT 2;

Exercise: List names and cities

Write a query that shows every employee’s emp_id, first_name and city, with city renamed to office.

Expected resultMySQL 8.4
emp_idfirst_nameoffice
100MeeraMumbai
101AshaBengaluru
102RahulBengaluru
103PriyaPune
104KaranBengaluru
105VikramMumbai
106NehaMumbai
107ArjunKolkata
108KavyaPune
109RohanPune
110SnehaDelhi
111AmitBengaluru
12 rows in set
Hint 1

Put the three columns after SELECT, separated by commas.

Hint 2

Rename a column with AS office.

Exercise: Monthly bonus

Show each employee’s first_name, salary and a column called bonus that is 10% of the salary.

Expected resultMySQL 8.4
first_namesalarybonus
Meera25000025000.00
Asha12500012500.00
Rahul980009800.00
Priya720007200.00
Karan720007200.00
Vikram10500010500.00
Neha540005400.00
Arjun480004800.00
Kavya880008800.00
Rohan450004500.00
Sneha780007800.00
Amit250002500.00
12 rows in set
Hint 1

10% of a number is the number multiplied by 0.10.

Quick quiz

1. Which clause does MySQL run first?

2. What does SELECT * return?

3. You run SELECT first_name job_title FROM employees. What happens?

4. Does SELECT change the data stored in the table?

Advertisement

SELECT FAQ

What is the SELECT statement in SQL?

SELECT is the SQL command that reads data from one or more tables. You list the columns you want after SELECT and the table after FROM, and the database returns a result set.

Is SELECT * bad practice?

It is fine for quick exploration, but in applications and reports list the columns you need. SELECT * transfers extra data and can break code when the table changes.

Can I use an alias from SELECT in the WHERE clause?

No. WHERE runs before SELECT, so the alias does not exist yet. Repeat the expression, or wrap the query in a subquery or CTE. You can use the alias in ORDER BY.

Is SQL case sensitive?

Keywords such as SELECT and FROM are not case sensitive, so select works too. Writing keywords in capitals is a convention that makes queries easier to read. Whether table names are case sensitive depends on the operating system and MySQL settings.

What is the order of execution of a SQL query?

FROM and JOIN, then WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY and finally LIMIT.