Archive for MySQL

SQL – Select Query with Clause Example

Sunday, November 22nd, 2009

SQL select query is used to fetch records from database. Select can use with clause and without clause. Select query returns number of rows from table. Select query can allow clause with where keyword. where clause is used for filter rows in database table.

SQL select clause query example explains how to use select clause query in SQL.

Table class_name with all records

+---------+-------------+------------+------------+
| Roll_No | Name        | Class_Name | Year_Start |
+---------+-------------+------------+------------+
|       1 | Amit Mishra | B Science  |       2005 |
|       2 | Robot       | B.Tech     |       2005 |
|       3 | Rohan       | M.Tech     |       2005 |
|       4 | Vivek       | B.Tech     |       2009 |
|       5 | Chander     | B.Science  |       2009 |
|       6 | Deepak      | B.Science  |       2009 |
+---------+-------------+------------+------------+

where clause to find equals to

select * from class_name where Class_Name = 'B.Tech';

select * from class_name where Class_Name <=> 'B.Tech';

Output

+---------+-------+------------+------------+
| Roll_No | Name  | Class_Name | Year_Start |
+---------+-------+------------+------------+
|       2 | Robot | B.Tech     |       2005 |
|       4 | Vivek | B.Tech     |       2009 |
+---------+-------+------------+------------+

returns only those row having class_name with B.Tech only

where clause to find not equals to

select * from class_name where Class_Name != 'B.Tech';

select * from class_name where Class_Name <> 'B.Tech';

Output

+---------+-------------+------------+------------+
| Roll_No | Name        | Class_Name | Year_Start |
+---------+-------------+------------+------------+
|       1 | Amit Mishra | B Science  |       2005 |
|       3 | Rohan       | M.Tech     |       2005 |
|       5 | Chander     | B.Science  |       2009 |
|       6 | Deepak      | B.Science  |       2009 |
+---------+-------------+------------+------------+

returns only those row not having class_name with B.Tech only

where clause to find less than to

select * from class_name where Year_Start < 2009;

Output

+---------+-------------+------------+------------+
| Roll_No | Name        | Class_Name | Year_Start |
+---------+-------------+------------+------------+
|       1 | Amit Mishra | B Science  |       2005 |
|       2 | Robot       | B.Tech     |       2005 |
|       3 | Rohan       | M.Tech     |       2005 |
+---------+-------------+------------+------------+

returns only those row in column Year_Start less than 2009 value only

where clause to find greater than to

select * from class_name where Year_Start > 2005;

Output

+---------+---------+------------+------------+
| Roll_No | Name    | Class_Name | Year_Start |
+---------+---------+------------+------------+
|       4 | Vivek   | B.Tech     |       2009 |
|       5 | Chander | B.Science  |       2009 |
|       6 | Deepak  | B.Science  |       2009 |
+---------+---------+------------+------------+

returns only those row in column Year_Start greater than 2005 value only

* indicates fetch all columns records from table.