SQL CLAUSE
1. WHERE
The WHERE Clause is used to retrieve only those records which fulfill the given criteria.
Syntax:
SELECT column_name
FROM table_name
WHERE conditions;
Example : Query using WHERE clause.
Consider the following table entitled
'Students', which contains information of the students.
Write the Query to find the record of the student where ID = 1.
Student_ID | LastName | FirstName | Marks |
---|
1 | Patil | Ravi | 60 |
2 | Morya | Surendra | 60 |
3 | Singh | Jaya | 78 |
4 | Pandit | Prajakta | 55 |
SELECT * FROM Students
WHERE Student_ID=1;
The result is shown in the following table.
Student_ID | LastName | FirstName | Marks |
---|
1 | Patil | Ravi | 60 |
2. AND
The SQL AND operator is used to combine multiple conditions along with WHERE clause.
Syntax:
SELECT Column_name
FROM table_name
WHERE condition
AND condition;
Example : Query using AND clause.
Consider the following table titled
'Clients'.
1. Write a query to select clients from Pune by using AND clause.
C_ID | LastName | FirstName | Contact_no | City | Country |
---|
1 | Patil | Ravi | 0201234568 | Pune | India |
2 | Morya | Surendra | 0202345677 | Pune | India |
3 | Singh | Jaya | 0203456788 | Berlin | Germany |
4 | Pandit | Prajakta | 0204567897 | Pune | India |
SELECT C_ID FROM Clients
WHERE City= 'Pune'
AND Country= 'India';
The result is shown in the following table.
2. Write a query to select the information of client from Pune by using AND clause.
SELECT * FROM Clients
WHERE City= 'Pune'
AND Country= 'India';
The result is shown in the following table.
C_ID | LastName | FirstName | Contact_no | City | Country |
---|
1 | Patil | Ravi | 0201234568 | Pune | India |
2 | Morya | Surendra | 0202345677 | Pune | India |
4 | Pandit | Prajakta | 0204567897 | Pune | India |
3. OR
The SQL OR operator is used to combine multiple conditions along with WHERE and OR clause.
Syntax:
SELECT * FROM table_name
WHERE condition
OR condition;
Example : Query using OR clause.
Write the query to select the FirstName from the following table titled
'Clients' by using OR operator.
C_ID | LastName | FirstName | Contact_no | City | Country |
---|
1 | Patil | Ravi | 0201234568 | Pune | India |
2 | Morya | Surendra | 0202345677 | Pune | India |
3 | Singh | Jaya | 0203456788 | Berlin | Germany |
4 | Pandit | Prajakta | 0204567897 | Pune | India |
SELECT FirstName FROM Clients
WHERE City='Pune'
OR City='Berlin';
The result is shown in the following table.
FirstName |
---|
Ravi |
Surendra |
Jaya |
Prajakta |
Write a query to extract data from the following table titled 'Clients' by using OR operator.
C_ID | LastName | FirstName | Contact_no | City | Country |
---|
1 | Patil | Ravi | 0201234568 | Pune | India |
2 | Morya | Surendra | 0202345677 | Pune | India |
3 | Singh | Jaya | 0203456788 | Berlin | Germany |
4 | Pandit | Prajakta | 0204567897 | Pune | India |
SELECT * FROM Clients
WHERE Country= 'India'
AND (City= 'Mumbai' OR 'Pune');
The result is shown in the following table.
C_ID | LastName | FirstName | Contact_no | City | Country |
---|
1 | Patil | Ravi | 0201234568 | Pune | India |
2 | Morya | Surendra | 0202345677 | Pune | India |
4 | Pandit | Prajakta | 0204567897 | Pune | India |