SQL SELECT Statement

SQL SELECT Statement

The most commonly used SQL command is SELECT statement. SQL SELECT statement is used to select data from database, mostly tables.

The data returned is stored in a table known as result-set. A query may retrieve data from only specified columns or from all of the columns in the table.

The simplest form of SELECT statement contains a list of column names and a table name.

SELECT Syntax

SELECT column_name1, column_name2, .... , column_nameN FROM table_name
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];
  1. table_name is the name of table from which we have to retrieve the data
  2. column_name1, column_name2, .... , column_nameN is the name of one or more columns from which we have to retrieve the data
  3. Other things which are included in square brackets are optional. You can read them later here SQL WHERE, SQL Group BY, SQL HAVING, SQL ORDER BY.

NOTE :
  • SQL keywords are NOT case sensitive: select is the same as SELECT
  • Some database systems require a semicolon at the end of each SQL statement.

Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

Consider a student_details table as given below.

 
id  | first_name | last_name | age |   subject   
----+------------+-----------+-----+-------------
  1 | Carol      | Dentist   |  15 | FoodTech
  2 | John       | Wick      |  19 | Architect
  3 | Daryl      | Dixon     |  13 | Mathematics
  4 | Clair      | Grimes    |  14 | Physics
  5 | Morgan     | Dee       |  15 | Wrestling

SELECT Example 1

The following SQL query is used to select the last name of all students.
 SELECT last_name FROM student_details; 

Output

  last_name 
-----------
 Dentist
 Wick
 Dixon
 Grimes 
 Dee
(5 rows)

SELECT Example 2

You can also retrieve data from more than one column. The following SQL query selects first name and last name of all the students.
 SELECT first_name, last_name FROM student_details; 

Output

 
  first_name | last_name 
------------+-----------
 Carol      | Dentist
 John       | Wick
 Daryl      | Dixon
 Clair      | Grimes 
 Morgan     | Dee
(5 rows)
You can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT statement.

NOTE: In SQL SELECT statement only SELECT and FROM statements are mandatory. Other clauses like WHERE, ORDER BY, GROUP BY, HAVING are optional.

SELECT Example 3

Expressions combine many arithmetic operators, they can be used in SELECT, WHERE and ORDER BY Clauses of the SQL SELECT Statement. When more than one arithmetic operators are used in an expression they evaluate they evaluate in a specific order.

The order of evaluation is: parentheses, division, multiplication, addition, and subtraction. The evaluation is performed from the left to the right of the expression.

The following SQL query calculates the result of expression.
SELECT 4*5 as result; 

Output

postgres=> select 4*5 as result;
 result 
--------
     20
(1 row)
Pre - SQL Introduction Next - SQL Aliases

Comments