SQL Select Statement
Select statement is query to fetch single or set of records from the data set. Select statement may consists of number of other important SQL clauses such as Distinct, Where, Limit, Group By or Order By. In select statement we can also use aggregative functions.
SELECT Statement Syntax
The fundamental and commonly used examples syntax of select statement is.
SELECT * | list_of_columns FROM table
Here select is the SQL clause keyword, after this we can select all columns of provided table for this purpose we will use * or provide list of specific columns. Here list of columns will be separated by comma's. After list of columns table name is provided that is unique in database.
Raw Database Table Example
For demonstrate examples of select statement or even other SQL clauses we are required a database schema. The following database table will be used for all such kind of queries.
Table name is employee from industry database.
id | name | dob | city | phone | blood_group |
1 | Jack | 1989-04-12 | New York | +155345342 | A+ |
2 | Milton | 1988-02-01 | Los Angeles | +154380323 | AB- |
3 | Devan | 1999-07-04 | Chicago | +124342321 | O+ |
4 | James | 1972-08-21 | Houston | +112340655 | B+ |
5 | Jack | 2002-03-24 | Portland | +133390232 | O- |
Important Note!.The above defined table is temporary and created using random values. This record does not belong to any person in real life. These all four records will be used only for SQL queries testing purpose.
How to Select Distinct Columns
Sometimes there are duplicate column values exist in particular field of database table. If we want to retrieve only unique values means each value only one time. This is called distinct selection. In SQL Distinct keyword is used before column identifier.
Syntax
SELECT DISTINCT column_name FROM table_name
Example
SELECT DISTINCT `name`,`dob` FROM employee
Output
The result of the above defined query will include single names record of each person. In databse table there are two persons having the same name "jack", but as the DISTINCT keyword is used so that the query will fetch and display only first occurrance of record having jack name.