Monday, February 9, 2015

SQL SELECT


SQL SELECT


The most commonly used SQL command is SELECT statement. It is used to query the database and retrieve selected data that follow the conditions we want.
In simple words, we can say that the select statement used to query or retrieve data from a table in the database.
Let's see the syntax of select statement.
  1. SELECT expressions  
  2. FROM tables  
  3. WHERE conditions;  
Here expression is the column that we want to retrieve.
Tables indicate the tables, we want to retrieve records from.

Optional clauses in SELECT statement

There are some optional clauses in SELECT statement:
[WHERE Clause] : It specifies which rows to retrieve.
[GROUP BY Clause] : Groups rows that share a property so that the aggregate function can be applied to each group.
[HAVING Clause] : It selects among the groups defined by the GROUP BY clause.
[ORDER BY Clause] : It specifies an order in which to return the rows.
For example, let a database table: student_details;
IDFirst_nameLast_nameAgeSubjectHobby
1AmarSharma20MathsCricket
2AkbarKhan22BiologyFootball
3AnthonyMilton25CommerceGambling
From the above example, select the first name of all the students. To do so, query should be like this:
  1. SELECT first_name FROM student_details;  
Note: the SQL commands are not case sensitive. We can also write the above SELECT statement as:
  1. select first_name from student_details;  
Now, you will get following data:
Amar
Akbar
Anthony
We can also retrieve data from more than one column. For example, to select first name and last name of all the students, you need to write
  1. SELECT first_name, last_name FROM student_details;  
Now, you will get following data:
AmarSharma
AkbarKhan
AnthonyMilton
We can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT statement.
Here a point is notable that only SELECT and FROM statements are necessary in SQL SELECT statements. Other clauses like WHERE, GROUP BY, ORDER BY, HAVING may be optional.

No comments:

Post a Comment