SQL SELECT Statement

  • The select statement is used to retrieve data from database‚Äôs tables.
  • The SQL select statement retrieves existing data from tables in the form of result-sets.
  • The select from statement can retrieve whole data from a table in a single statement.
  • You can also fetch specific columns or field data by using Select query.
  • Rather fetching whole table‚Äôs data you can limit fetched records, as per need, by using Where clause with select statement
  • The Select statement can also fetch data from more than one tables in single select statement, explained in join table chapter.

Let's see the syntax of select statement.

SQL SELECT Syntax

SELECT column_name,column_name

FROM table_name;

or


SELECT * FROM table_name;




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;

ID First_name Last_name Age subject Hobby
1 Mithilesh Singh 25 English Cricket
2 Harish Sharma 26 Biology Running
3 Rahul Singh 25 Commerce Sleeping

From the above example, select the first name of all the students. To do so, query should be like this:

SELECT first_name FROM student_details;

Note: the SQL commands are not case sensitive. We can also write the above SELECT statement as:

select first_name from student_details;

Now you will get data following

Mithilesh
Harish
Rahul

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

SELECT first_name, last_name FROM student_details;

Now get following data:

Mithilesh Singh
Harish Sharma
Rahul Singh

We can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT statement.

We can also use clauses like WHERE, GROUP BY, HAVING, ORDER BY with SELECT statement.