Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 49

CLAUSES IN SQL

By : MD

Facilitator : Ghan Sir


CONTENTS

1. Meaning of SQL
2. Meaning of Clauses
3. Different Clauses in SQL
4. Conclusion
5. References
Query Language,
Definition and manipulation Language,
Accessing, modifying and extraction of information.
1) SQL
Built-in Functions
Statements 2) Clauses
a) Where Clause
b) Order By Clause
c) Group By Clause 3) Types
d) Having Clause
Filters data
Conditional queries a) Where
Comparison Operator Description
= Equal to
<> , != Not Equal to
<  Less than
<= Less than or Equal to
>  Greater than
>= Greater than or Equal to
AND Logical AND
OR Logical OR
NOT Logical NOT
BETWEEN Between two specified values (inclusive)
NOT BETWEEN Not between two specified values
IN Used for multiple OR, Matches a value in a list
NOT IN Matched a value outside a list
IS NULL NULL value
IS NOT NULL Non-NULL value
LIKE Used for pattern matching with % and _
EXISTS Condition is met if subquery returns at least one row
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the names of student whose age is 23
SELECT Sname
Equal to
FROM Student
WHERE Age=23;

Find the names of student is from Vyas-07


SELECT Sname
FROM Student
WHERE Saddress=‘Vyas-07’;
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the address of students whose age is not 16 Not Equal to
SELECT Saddress
FROM Student
WHERE Age<>16;

Find the names and age of students who don’t live in Vyas-08
SELECT Sname, Age
FROM Student
WHERE Saddress!=‘Vyas-08’;
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the SID and address of students having age less than 20 Less than
SELECT SID, Saddress
FROM Student
WHERE Age<20;

Find the name of students whose age is less than 20 and lives in Vyas-11
SELECT Sname
FROM Student
WHERE Age<20
AND Saddress=‘Vyas-11’;
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the name of students having age less than or equal to 21 Less than or equal to
SELECT Sname
FROM Student
WHERE Age<=21;

Find the address of students whose SID is less than or equal to S13
SELECT Saddress
FROM Student
WHERE SID<=‘S13’;
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the name of students having age greater than 40
SELECT Sname
Greater than
FROM Student
WHERE Age>40;

Find the address of students whose SID is greater than S14


SELECT Saddress
FROM Student
WHERE SID>’S14’;
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the name of students having age greater than or equal to 25 Greater than or equal to
SELECT Sname
FROM Student
WHERE Age>=25;

Find the address of students whose SID is greater than or equal to 12


SELECT Saddress
FROM Student
WHERE SID>=‘S12’;
AND
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the name of students whose age is 23 and lives in Pokhara
SELECT Sname
AND
FROM Student
WHERE Age=23 AND Saddress=‘Pokhara’;

Find the address of students whose SID is greater than 12 and lives in Ktm
SELECT Saddress
FROM Student
WHERE SID>’S12’ AND Saddress=‘Ktm’;
OR
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the name of students living in Kathmandu or having age greater than 20
SELECT Sname
OR
FROM Student
WHERE Saddress=‘Ktm’ OR Age>20;

Find the address of students whose SID is greater than 12 or who lives in
Ktm
SELECT Saddress
FROM Student
WHERE SID>’S12’ OR Saddress=‘Ktm’;
NOT
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the address of students whose age is not 16
SELECT Saddress
NOT
FROM Student
WHERE NOT Age=16;

Find the names and age of students who are not Pokhareli
SELECT Sname, Age
FROM Student
WHERE NOT Saddress=‘Pokhara’;
BETWEEN
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the name and address of students whose age is between 16 and 20 BETWEEN
SELECT Sname, Saddress
FROM Student
WHERE Age BETWEEN 16 AND 20;

Find the age of students whose dept ID is between D85 and D90
SELECT Age
FROM Student
WHERE SDepID BETWEEN ‘D85’ AND ‘D90’;
NOT BETWEEN
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the name and address of students whose age is not between 16 and 20 NOT BETWEEN
SELECT Sname, Saddress
FROM Student
WHERE Age NOT BETWEEN 16 AND 20;

Find the age of students whose dept ID is not between D85 and D90
SELECT Age
FROM Student
WHERE SDepID NOT BETWEEN ‘D85’ AND ‘D90’;
IN
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find all the records of stds who live in Ktm or Palpa or Banepa. IN
SELECT *
FROM Student
WHERE Saddress IN (‘Ktm’, ‘Palpa’, ‘Banepa’);

Find the name of students whose age is 16 or 23 or 45.


SELECT Sname
FROM Student
WHERE Age IN (16, 23, 45);
NOT IN
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find all the records of stds who doesn’t live in Ktm or Palpa or Banepa.
SELECT *
NOT IN
FROM Student
WHERE Saddress NOT IN (‘Ktm’, ‘Palpa’, ‘Banepa’);

Find the name of students whose age is not 16 or 23 or 45.


SELECT Sname
FROM Student
WHERE Age NOT IN (16. 23. 45);
IS NULL
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find all the records of stds whose address is not available IS NULL
SELECT *
FROM Student
WHERE Saddress IS NULL;

Find the name of students whose address is not available


SELECT Sname
FROM Student
WHERE Saddress IS NULL;
IS NOT NULL
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find all the records of stds whose address is available IS NOT NULL
SELECT *
FROM Student
WHERE Saddress IS NOT NULL;

Find the name of students whose age is available


SELECT Sname
FROM Student
WHERE Age IS NOT NULL;
WILDCARD OPERATORS
% Allows to match any string of any length
_ Allows to match on a single character

Examples
A% Data starting with A (No length limit)
%A Data ending with A (No length limit)
%A% Data with A somewhere between (No length limit)
A%A Data starting and ending with A (No length limit) LIKE
A_ __ _ Data starting with A (Length limit = 5 characters)
__ __A Data ending with A (Length limit = 5 characters)
__A__ Data with A somewhere between ( Limit = 5 characters)
A_ __A Data starting and ending with A (Limit = 5 characters)
LIKE
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find all the records of stds whose name starts with A
SELECT *
LIKE (%)
FROM Student
WHERE Sname LIKE ‘A%’;

Find the address of students whose name end with A


SELECT Saddress
FROM Student
WHERE Sname LIKE ‘%A’;
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find all the records of stds whose name starts with A and has 5 characters
SELECT *
LIKE ( _ )
FROM Student
WHERE Sname LIKE ‘A _ _ _ _’;

Find the address of students whose name end with A and has 5 characters
SELECT Saddress
FROM Student
WHERE Sname LIKE ‘_ _ _ _ A’;
EXISTS
Sorting data’s b) ORDER BY
b) ORDER BY
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the address of stds whose name starts with A in ascending order of name b) ORDER BY
SELECT Saddress
FROM Student
WHERE Sname LIKE ‘A%’
ORDER BY Sname ASC;

Find the name of stds of age betn 16 and 25 in descending order of address
SELECT Sname
FROM Student
WHERE Age BETWEEN 16 AND 25
ORDER BY Saddress DESC;
Grouping identical data’s c) GROUP BY
c) GROUP BY
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16

Group the students in their addresses c) GROUP BY


SELECT Sname
FROM Student
GROUP BY Saddress;

Find the no of stds in each address


SELECT COUNT(Saddress)
AS No_of_stds, Saddress
FROM Student
GROUP BY Saddress;
Filters data after their grouping d) HAVING
d) HAVING
Student
SID Sname Saddress SDepID Age
S10 Maya Vyas-09 D90 22
S11 Abin Vyas-11 D89 17
S12 Aarav Vyas-10 D88 21
S13 Ashna Vyas-08 D87 45
S14 Anuj Vyas-08 D86 23
S15 Manish Vyas-07 D85 23
S16 Pinky Vyas-07 D84 16
Find the names of stds in same address with age greater than 20 d) HAVING
SELECT Sname
FROM Student
GROUP BY Saddress
HAVING Age>20;

Find the name of stds in each address with maximum age


SELECT COUNT(Saddress) AS No_of_stds, Sname, Age, Saddress
FROM Student
GROUP BY Saddress
HAVING MAX(Age);
CONCLUSION

We use SQL for storing, managing, manipulating, retrieving, extracting data
from the database. It is very useful as it gives its users the privilege of
modifying data in the database. The user can use its different clauses to meet
the condition and get the desired output from the database.
 https://www.techtarget.com/searchdatamanagement/definition/SQL#:~:text=Structur
ed%20Query%20Language%20(SQL)%20is,on%20the%20data%20in%20them.
 https://www.scaler.com/topics/clause-in-sql/
 https://www.tutorialspoint.com/sql/sql-where-clause.htm#:~:text=The%20SQL%20
WHERE%20clause%20is,filtered%20data%20from%20the%20table
.
 https://www.techonthenet.com/sql/and.php
 https://www.w3schools.com/sql/sql_exists.asp
 https://www.w3schools.com/sql/sql_syntax.asp
 https://www.javatpoint.com/dbms-sql-command

References
 https://www.freecodecamp.org/news/basic-sql-commands/
 https://www.geeksforgeeks.org/sql-ddl-dql-dml-dcl-tcl-commands/
 https://www.codecademy.com/article/sql-commands
 https://www.scaler.com/topics/dbms/sql-commands/
 https://www.geeksforgeeks.org/sql-having-clause-with-examples/
 https://www.tutorialspoint.com/sql/sql-having-clause.htm
 https://www.tutorialspoint.com/sql/sql-order-by.htm#:~:text=The%20SQL%20ORD
ER%20BY%20clause,column%20or%20by%20multiple%20columns
.
 https://
www.ibm.com/docs/en/informix-servers/14.10?topic=clauses-group-by-clause

You might also like