SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Select - SQL基础教程 - 笔下光年
网站首页
SQL Select
In this tutorial you will learn how to select records from database tables using SQL. ## Selecting Data from Table In the previous chapter we've learned how to insert data in a database table. Now it's time to select the data from existing tables using the SQL query. The `SELECT` statement is used to select or retrieve the data from one or more tables. You can use this statement to retrieve all the rows from a table in one go, as well as to retrieve only those rows that satisfy a certain condition or a combination of conditions. ## Syntax The basic syntax for selecting the data from a table can be given with: ```sql SELECT column1_name, column2_name, columnN_name FROM table_name; ``` Here, column1_name, column2_name, ... are the names of the columns or fields of a database table whose values you want to fetch. However, if you want to fetch the values of all the columns available in a table, you can just use the following syntax: ```sql SELECT * FROM table_name; ``` Let's put these statements into real use. Suppose we've a table named employees in our database that 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 | +--------+--------------+------------+--------+---------+ ``` ### Select All from Table The following statement will return all the rows from the employees table. ```sql SELECT * FROM employees; ``` After execution, the output will look something like this: ``` +--------+--------------+------------+--------+---------+ | 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 returns all the rows and columns from the employees table. <div class="callout callout-info mb-3">Tip: The asterisk (*) is a wildcard character that means everything. For example, the asterisk character in the SELECT statement of the example above is a shorthand substitute for all the columns of the employees table.</div> ### Select Columns from Table If you don't require all the data, you can select specific columns, like this: ```sql SELECT emp_id, emp_name, hire_date, salary FROM employees; ``` After executing the above statement, you'll get the output something like this: ``` +--------+--------------+------------+--------+ | 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 no dept_id column in the result set. In the next chapter we'll learn how to select the records from a table based on a condition.
上一篇:
SQL Insert
下一篇:
SQL Where