Saturday, 30 September 2017

SQL SELECT Statement



The SELECT statement is used to select or display data from a database.

Syntax:


SELECT [DISTINCT] [*] {COL1 [ALIAS],...}
FROM TABLE_NAME;
  • SELECT keyword Specifies the list of column.
  • DISTINCT keyword show you unique data(suppresses the duplicate data). 
  • "*" projects all the column from selected table.
  • ALIAS hide the original column name and we can use alias for table name .
  • FROM keyword Specifies the table name.


Example:

Selecting all columns.



SELECT * FROM STUDENTS;

Selecting list of column.


SELECT STUDNO,FNAME,LNAME,DOB FROM STUDENTS;

Using Alias name to hide the column name.


SELECT STUDNO as "Student ID",FNAME as "First Name",LNAME AS "Last Name",DOB AS "Date Of Birth"
FROM STUDENTS;

Using Alias name for table.


SELECT A.STUDNO as "Student ID",A.FNAME as "First Name",A.LNAME AS "Last Name",A.DOB AS "Date Of Birth"
FROM STUDENTS A;


The SELECT statement has many optional clauses:

WHERE specifies which rows to retrieve.
GROUP BY groups rows sharing a property so that an aggregate function can be applied to
 each group.
HAVING selects among the groups defined by the GROUP BY clause.
ORDER BY specifies an order in which to return the rows.


No comments:

Post a Comment