Introduction
SQL SELECT statement is used to query or retrieve data from a table in the database. A query may retrieve information from specified columns or from all of the columns in the table. SQL commands are not case sensitive.
Syntax
General syntax of SELECT is
SELECT column_list FROM table_name [WHERE Condition] [ORDER BY clause];
- table_name is the name of the table from which data is retrieved.
- column_list includes one or more columns from which data is retrieved.
- Code within the brackets are optional.
Column names that follow the SELECT keyword determine which columns will be returned in the results. Use ‘*’ to select all columns. Table name that follows the keyword FROM specifies the table that will be queried to retrieve the desired results.
WHERE clause (optional) specifies which data values or rows will be returned or displayed, based on the condition described after the keyword. Conditional selections used WHERE clause:
- Greater than (>)
- Greater than or equal (>=)
- Less than (<)
- Less than or equal (<=)
- Equal (=)
- Not equal (<>)
Example
Consider below table
Dump all the entries from the table.
SELECT * FROM employleinfo /* Result of Query */ id firstName lastName age city 1 Rahul Bhagwat 20 Noida 2 Stephen Fleming 17 Delhi 3 Sebastian Smith 19 Patna 4 Anjali Sharma 50 Mumbai 5 Erica Edwards 40 Chennai 6 Priya Gowda 50 Pune 7 Shekar Chandra 43 Delhi 8 Shekar Sharma 19 Patna
List the first name and last name of all employee.
SELECT firstName, lastName FROM employleinfo /* Result of Query */ firstName lastName Rahul Bhagwat Stephen Fleming Sebastian Smith Anjali Sharma Erica Edwards Priya Gowda Shekar Chandra Shekar Sharma
Query all the employee who live in a particular city.
SELECT firstName, lastName FROM employleinfo WHERE city='Patna' /* Result of Query */ firstName lastName Sebastian Smith Shekar Sharma