www.SQLdemon.com    
  Home | Place like no other Learn | Things you should know Labs | Try what you know Stuff | Things you may need Blog | Latest things to discuss
 

Retrieving Columns

The syntax below shows the clauses that work with the select statement.

SELECT [Distinct | DISTINCTROW | ALL] select_expression, …
[FROM table_references
[WHERE where_definition]
[GROUP BY group_by_expression]
[HAVING where_definition]
[ORDER BY {unsigned_integer | col_name | formula} | ASC | DESC]
[LIMIT [offset] rows]

Please refer “The SELECT Statement” for the explanation of this syntax

 

Select all columns from the table


Let us understand the functionality of the SELECT by retrieving the data from the following table.

Employee Table

The statement that will return the table above is;

SELECT Employee_ID, First_Name, Last_Name, Email, Phone_Number, Hire_date, Job_ID, Salary
FROM Employees;

We can also rewrite this statement with “*” sign which will work as a wild card to return all the columns.

SELECT * FROM Employees;



Using alias to define column names


If you analyse the figure above, you would have already noticed that the column names are returned as how it is defined in tables already, but we can customize them according to our requirements using an alias.

You can define an alias by “AS” keyword,

SELECT Employee_ID AS "Emp ID", First_Name AS "First Name", Last_Name AS "Last Name", Email, Phone_Number AS "Phone No", Hire_date AS "Hire Date", Job_ID AS "Job ID", Salary AS "Salary"
FROM Employees;

But major database management systems support without the keyword in that case you can define within quotes followed by the column name.

SELECT Employee_ID "Emp ID", First_Name "First Name", Last_Name "Last Name", Email, Phone_Number "Phone No", Hire_date "Hire Date", Job_ID "Job ID", Salary "Salary"
FROM Employees;

Results with alias

Select all from employees with Alias