Using SELECT Statement with WHERE clause - SQL Query
Clause are used to filter the record by specifying some conditions.
1. WHERE CLAUSE
Consider the following table 'Stationary'.
ID | Name | Quantity | Price |
---|
1 | Laptop | 2 | 70000 |
2 | Desktop | 2 | 60000 |
3 | Speakers | 5 | 15000 |
4 | UPS | 10 | 20000 |
5 | Tablets | 2 | 20000 |
I) Display the records where Price = 20000 from table 'Stationary'.
To display the records which are equal to 20000, the query should be written as:
SELECT * FROM Stationary
WHERE Price = 20000
Result:
ID | Name | Quantity | Price |
---|
4 | UPS | 10 | 20000 |
5 | Tablets | 2 | 20000 |
Note: Following operators are used in the WHERE clause.
Operator | Description |
---|
'=' | Equal |
'<>' | Not equal |
'>' | Greater than |
'>=' | Greater than or equal |
'<=' | Less than or equal |
2. AND & OR & WHERE clause with SELECT Statement
Consider the following table 'Students'.
Stud_ID | Name | Phone | City | Country |
---|
1 | Alex | 654124 | Perth | Australia |
2 | Martin | 654125 | Sydney | Australia |
3 | Shruti | 910001 | Delhi | India |
4 | Jaya | 910002 | Mumbai | India |
5 | Paul | 450525 | London | England |
6 | Andrew | 450526 | London | England |
I) Display the Stud_ID from table Students, where City = 'Delhi' and Country = 'India'.
The query should be written as:
SELECT Stud_ID FROM Students
WHERE City = 'Delhi'
AND Country = 'India'
Result:
II) Display the records of the students who are from country Australia and England.
To display the records of the students who are from country Australia and England. The query should be written as:
SELECT * FROM Students
WHERE Country = 'Australia'
OR Country = 'England'
Result:
Stud_ID | Name | Phone | City | Country |
---|
1 | Alex | 654124 | Perth | Australia |
2 | Martin | 654125 | Sydney | Australia |
5 | Paul | 450525 | London | England |
6 | Andrew | 450526 | London | England |
iii) Display the records of student who are from the city Delhi or Mumbai.
To display the records of the students who are from Delhi and Mumbai. So, both cities are belongs to country India. The query should be written as:
SELECT * FROM Students
WHERE Country = 'India'
AND (City= 'Delhi' OR City ='Mumbai')
Result:
Stud_ID | Name | Phone | City | Country |
---|
3 | Shruti | 910001 | Delhi | India |
4 | Jaya | 910002 | Mumbai | India |