SQL - SELECT Queries

The SELECT queries retrieve rows/columns data from one or more existing tables.


SELECT column1, column2,...columnN 
FROM table_name

For the demo purpose, the following Employee table will be used in all the examples here.

EmpId FirstName LastName Email PhoneNo Salary
1 'John' 'King' '[email protected]' '650.127.1834' 33000
2 'James' 'Bond'
3 'Neena' 'Kochhar' '[email protected]' '123.456.4568' 17000
4 'Lex' 'De Haan' '[email protected]' '123.456.4569' 15000

Select All Columns

The * operator represents all the columns of a table. So, you don't need to specify each column name in the SELECT query to get data from all the columns.

SQL Script: Select Query
SELECT * FROM Employee;

The above query returns all the rows and columns data from the Employee table, as shown below.

EmpId FirstName LastName Email PhoneNo Salary
1 'John' 'King' '[email protected]' '650.127.1834' 33000
2 'James' 'Bond'
3 'Neena' 'Kochhar' '[email protected]' '123.456.4568' 17000
4 'Lex' 'De Haan' '[email protected]' '123.456.4569' 15000

Select Specific Columns Data

Specify the column names in the SELECT statement to get the data only from those columns, as shown below.

SQL Script: Select Query
SELECT EmpId, FirstName, LastName FROM Employee;
EmpId FirstName LastName
1 'John' 'King'
2 'James' 'Bond'
3 'Neena' 'Kochhar'
4 'Lex' 'De Haan'

Alias for Columns and Table

You can specify an alias for one or more columns in the SELECT query. An alias is a temporary name to a table or column in the query.

Advantage of Alias:

  • Alias makes a column more readable in the result set.
  • Alias is used to give a small, abbreviated, and meaningful name to tables in the query so that it will be easy to refer tables in joining multiple tables.
  • Alias helps us to identify which column belongs to which table in case of getting data from multiple tables.

The following query specifies the "Employee Id" for EmpId, and Name as an alias for the FirstName column in the SELECT query. Specify an alias in the single or double quotes if you want a space or other string in it.

SQL Script: Column Alias
SELECT EmpId "Employee Id", FirstName AS Name FROM Employee;
Employee Id Name
1 'John'
2 'James'
3 'Neena'
4 'Lex'

Operators in SELECT Statement

You can specify the operators in the select statement to perform some action on the column value. For example, the + operator in MS SQL Server and || operator in PostgreSQL, MySQL, and Oracle database concatenates string values or adds numeric values. The following concatenates two varchar columns in the result.

SQL Script: Select Query with Operators in SQL Server
SELECT EmpId, FirstName + ' ' + LastName AS "Full Name" 
FROM Employee;
EmpId Full Name
1 'John King'
2 'James Bond'
3 'Neena Kochhar'
4 'Lex De Haan'

FROM Clause

The SELECT statement must have the FROM clause. The FROM clause is used to list down table names from which we want to select data and specify joins between those tables.

You can specify multiple tables in the FROM clause to select data from. However, if tables have the same columns, then you need to specify full column names like table_name.column_name in the SELECT query.

The following selects columns from two tables.

SQL Script: Select Data From Multiple Tables
SELECT * FROM Employee, Deparatment;

SELECT Employee.*, Department.* FROM Employee, Deparatment;

SELECT emp.*, dept.* FROM Employee emp, Deparatment dept; 

SELECT emp.FirstName, dept.DipartmentName FROM Employee emp, Deparatment dept;

Having multiple tables in the FROM clause without the WHERE clause and JOIN would return repeated data from each table. For example, if the Employee table has two rows and the Department table has two rows, then the above query would return four rows where columns of one table would repeat for the columns of another table. You will learn more about it in the JOIN tutorial.