Last updated on May 11th, 2023
Estimated reading time: 2 minutes
Syntax
The select statement in SQL is used to retrieve data from database. Select is most commonly used SQL statement. We can either fetch entire table or specific columns based on select query. The output is stored in result table name as result set.
Retrieve specific columns from table.
select column1,column2,column3,.....
from tablename;
column1,column2 are the list of columns that are required in result set and tablename is the name of database table.
Retrieve all columns from table.
select * from tablename;
Database Query Example
We have created new table Employee in HRMS DB.
Select all records from Employee table.
select * from Employee
Select EmployeeId and firstname columns.
select EmployeeId,Firstname from Employee
SQL Aliases
SQL aliases are used to give a column and table temporary name. An alias is created with the AS
keyword. It is used to make column name more readable.
Table aliases are useful in complex SQL joins when there are more than 1 tables.
SELECT column1 as columnname
FROM tablename as t;
SQL Aliases Example
select e.EmployeeId,e.Firstname as EmployeeName, e.Email as EmployeeEmail from Employee e
Column aliases are useful in case of long column names ,Concat of multiple columns or calculated columns using SQL functions.
select Firstname+lastname as EmployeeName from Employee