English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use SQL to select records from a database table.
In the previous chapter, we learned how to insert data into a database table. Now it's time to use SQL queries to select data from existing tables.
The SELECT statement is used to select or retrieve data from one or more tables. You can use this statement to retrieve all rows in a table at one time, as well as retrieve those rows that meet specific conditions or combinations of conditions.
The basic syntax for selecting data from a table can be given as follows:
SELECT column1_name, column2_name, columnN_name FROM table_name;
Here,column1_name,column2_name,...is the name of the database table column or field you want to retrieve its value. However, if you want to retrieve the values of all available columns in the table, you can use the following syntax:
SELECT * FROM table_name;
Let's put these statements into practical use. Suppose we have a database namedemployeesThe table, which contains the following records:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+--------------+------------+--------+---------+
The following statement will returnemployeesAll rows in the table.
SELECT * FROM employees;
After execution, the output will be as follows:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 4 | | 2 | Tony Montana | 2002-07-15 | 6500 | 1 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 5 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 3 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+--------------+------------+--------+---------+
As you can see, it returnsemployeesAll rows and columns in the table.
Tip:Asterisk (*)is a wildcard, indicating all content. For example, the asterisk character in the statement above the SELECT example isemployeeAbbreviations of all columns in the table.
If you do not need all the data, you can select specific columns as shown below:
SELECT emp_id, emp_name, hire_date, salary FROM employees;
After executing the above statement, you will get the following output:
+--------+--------------+------------+--------+ | emp_id | emp_name | hire_date | salary | +--------+--------------+------------+--------+ | 1 | Ethan Hunt | 1995-10-30 | 5000 | | 2 | Tony Montana | 1990-07-15 | 6500 | | 3 | Sarah Connor | 2011-04-13 | 5600 | | 4 | Rick Deckard | 2005-10-18 | 7200 | | 5 | Martin Blank | 1996-05-24 | 8000 | +--------+--------------+------------+--------+
As you can see, this time there is nodept_idIn the next chapter, we will learn how to select records from a table based on conditions.