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'.

IDNameQuantityPrice
1Laptop270000
2Desktop260000
3Speakers515000
4UPS1020000
5Tablets220000

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:

IDNameQuantityPrice
4UPS1020000
5Tablets220000

Note: Following operators are used in the WHERE clause.

OperatorDescription
'='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_IDNamePhoneCityCountry
1Alex654124PerthAustralia
2Martin654125SydneyAustralia
3Shruti910001DelhiIndia
4Jaya910002MumbaiIndia
5Paul450525LondonEngland
6Andrew450526LondonEngland

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:

Stud_ID
3

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_IDNamePhoneCityCountry
1Alex654124PerthAustralia
2Martin654125SydneyAustralia
5Paul450525LondonEngland
6Andrew450526LondonEngland

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_IDNamePhoneCityCountry
3Shruti910001DelhiIndia
4Jaya910002MumbaiIndia