Learn MySQL for Developing Web Application

Learn to build web applications with PHP and MySQL, Start your own blog, e-commerce site. In this tutorial you will learn queries of MySQL and MySQLi.

SQL SELECT Query


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.

SELECT expressions
FROM tables
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
1RiteshMalhotra30HistoryCricket
2SalmanKhan25MathsRugby
3JabyMilton28CommerceTennis

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 following data:

Ritesh
Salman
Jaby

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, you will get following data:

RiteshMalhotra
SalmanKhan
JabyMilton

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.