0% found this document useful (0 votes)
9 views12 pages

Clause DBMS Yshword

Uploaded by

freefire206354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Clause DBMS Yshword

Uploaded by

freefire206354
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

WHERE Clause:

The WHERE clause restricts the records that do not match the specified condition. It eliminates
unmatched records from the table.

In short, the WHERE clause restricts records that do not meet the conditions specified in the
WHERE clause. It is used to apply conditions in DBMS (Database Management Systems).

The WHERE clause is used with the following commands in DBMS:

 SELECT
 DELETE
 UPDATE

Syntax:

SELECT * FROM table_name WHERE condition;

We have the following table:

SQL> SELECT * FROM player;


Rank Name Best

1 Virat 183

2 Dhoni 183

3 Rohit 264

4 Dhawan 143

5 Sachin 219

Example Queries:

1. If we want to select the player with rank 3, the query will be:

SQL> SELECT * FROM player WHERE rank = 3;

Output:

Rank Name Best

3 Rohit 264
2. If we want to show only the name of the player with rank 3, the query will be:

SQL> SELECT name FROM player WHERE rank = 3;

Output:

Name

Rohit

UPDATE:

To update records in a table, use the UPDATE command. The syntax is:

UPDATE table_name SET column_name = new_value WHERE condition;

Example:

SQL> UPDATE player SET name = 'Sachin' WHERE best = 219;


SQL> SELECT * FROM player;

Output:

Rank Name Best

1 Virat 183

2 Dhoni 183

3 Rohit 264

4 Dhawan 143

5 Sachin 219

DELETE:

To delete records from a table, use the DELETE command. The syntax is:

DELETE FROM table_name WHERE condition;

Example:

To delete the row where rank is 4:


SQL> DELETE FROM player WHERE rank = 4;
SQL> SELECT * FROM player;

HAVING Clause:

 The HAVING clause restricts groups. In short, it is used to filter groups.


 It is always used with the GROUP BY clause and is evaluated after the groups are formed.
 It eliminates groups that do not match the HAVING condition.

Example:

We have the following table:

SQL> SELECT * FROM emp;


Eid Ename Esal
1 Sales 10000
2 HR 15000
3 Sales 20000
4 Sales 10000
5 HR 23000
6 Product 25000
7 Sales 25000
8 Product 12000
9 Sales 18000
10 Sales 37000
11 HR 63000

If we want to know how many departments there are in the table, without using the HAVING
clause, we write:

SQL> SELECT dname FROM emp GROUP BY dname;

Output:

Dname
Sales
HR
Product

If we want to show the count of Sales, HR, and Product departments:

SQL> SELECT dname, COUNT(*) FROM emp GROUP BY dname;


Output:

Dname Count(*)
Sales 6

HR 3

Product 2

If we want to show departments with 3 or more employees:

SQL> SELECT dname, COUNT(*) FROM emp HAVING COUNT(*) >= 3 GROUP BY dname;

Output:

Dname Count(*)
Sales 6
HR 3

If we want to show the names and salaries of employees earning more than 30000:

SQL> SELECT dname, esal FROM emp HAVING esal > 30000 GROUP BY dname, esal;

Output:

Dname Esal
Sales 37000
HR 63000

JOINS:

The purpose of JOIN is to combine data from multiple tables.

A JOIN is performed using the WHERE clause, which combines specified rows of the tables. If a
record is matched, it is joined.

If a JOIN involves more than two tables, it first joins the first two tables based on the JOIN
condition, and then compares the result with the next table, and so on.
Table 1: dept

Did Dname Location

10 Sales Delhi

20 HR Goa

30 Product Mumbai

Table 2: emp

Empno Ename Job Did

111 Rohit Analyst 10

222 Ankit Clerk 20

333 Neha Manager 10

444 Ram Teacher 40

TYPES OF JOIN:

 Equi Join
 Non-Equi Join
 Self Join
 Natural Join
 Cross Join
 Outer Join
o Left Outer Join
o Right Outer Join
o Full Outer Join
 Inner Join

Example:

We have the following tables:

SQL> SELECT * FROM personal;


Id Name Address Contact
10 Ankit Kapali 2845236189
Id Name Address Contact
20 Ankush Kapali 2845235491
30 Akhilesh Mango 4537235491
40 Altaf Sakchi 4567467490

SQL> SELECT * FROM professional;

Work Dept Id
Teacher BCA 20
Doctor MBBS 40
Teacher MBA 50
Programmer JAVA 60
Cashier ICICI 10

EQUI JOIN:

An equi join is a join in which the = symbol is used to match rows.

Example:

SQL> SELECT name, address, work FROM personal, professional WHERE personal.id
= professional.id;

Output:

Name Address Work


Ankush Kapali Teacher
Altaf Sakchi Doctor
Ankit Kapali Cashier

Natural Joins: If we want to select only name, contact, and dept from two tables that have the
same column, we can use a natural join.

Example:

SELECT name, contact, dept FROM personal NATURAL JOIN professional;


Name Contact Dept
Ankush 2845235491 BCA
Altaf 4567467490 MBBS
Ankit 2845236589 ICICI

Note: Natural join and equi join are the same. It only shows matched records.

Inner Join: Equi join, natural join, and inner join work the same way, displaying only the
matched records.

Example:

SELECT name, contact, dept FROM personal INNER JOIN professional ON


personal.id = professional.id;
Name Contact Dept
Ankush 2845235491 BCA
Altaf 4567467490 MBBS
Ankit 2845236589 ICICI

Cross Join: Used to combine every record from the first table with every record from the second
table, resulting in a Cartesian product.

Example:

SELECT name, contact, dept FROM personal CROSS JOIN professional;

Name Contact Dept


Ankit 0123456789 BCA
Ankit - MBBS
Ankush 0123456789 ICICI
Altaf 0123456789 BE
Ritesh 0123456789 BCA

Outer Join: 1. Left Outer Join: The left outer join returns all records from the left table
(personal), and matched records from the right table (professional). If there’s no match, NULL
values are shown.

Example:
SELECT name, address, dept FROM personal LEFT OUTER JOIN professional ON
personal.id = professional.id;
Name Address Dept
Ankush Kapali BCA
Altaf Sakchi MBA
Ankit Kapali ICICI
Akhilesh Mango NULL

Self Join: A self join is used to join a table to itself, typically by comparing two columns from
the same table.

Example:

SELECT p1.name, p1.address, p2.contact

FROM personal p1, personal p2

WHERE p1.id = p2.age;

Name Address Contact


Ankush Kapali 2845235491
Akhilesh Mango 4567235491

SQL Operators

1. Arithmetic Operators:

 *: Multiplication
 +: Addition
 -: Subtraction
 /: Division

Example: To increase the salary of an employee by 3000:

UPDATE emp SET esal = esal + 3000 WHERE eid = 50;

Eid Ename Esal Doj

10 Ankit 20000 15-Aug-2000

50 Rohit 30000 12-Feb-2007


2. Relational Operators: =, >, <, <=, >=

Example: To display employees with salary less than 20000:

SELECT * FROM emp WHERE esal < 20000;


Eid Ename Esal Doj
20 Ankush 10000 12-Sep-2000
60 Altaf 10000 25-Mar-2003

3. Logical Operators:

 AND: Both conditions must be true.


 OR: Either condition can be true.
 NOT: Negates a condition.

Example: To display employees with salary 20000 and joining date '15-Aug-2000':

SELECT * FROM emp WHERE esal = 20000 AND doj = '15-Aug-2000';


Eid Ename Esal Doj

10 Ankit 20000 15-Aug-2000

SQL Set Operators

Set operators allow operations between two or more tables, producing results based on the
chosen operator.

 UNION: Combines two result sets and eliminates duplicates.


 UNION ALL: Combines two result sets including duplicates.
 INTERSECT: Returns only the common records between two result sets.
 MINUS: Returns records from the first result set that are not present in the second.

Example (UNION):

SELECT * FROM stu1 UNION SELECT * FROM stu2;


Sroll Sname
10 Ankit
20 Alok
30 Akhilesh

Example (INTERSECT):

SELECT * FROM stu1 INTERSECT SELECT * FROM stu2;

Sroll Sname

30 Akhilesh

SQL Functions

SQL provides two main types of functions:

1. Aggregate/Group Functions (Multi-row)


2. Single-row Functions

1. Aggregate Functions:

 COUNT(): Returns the number of rows.


 SUM(): Returns the sum of a numeric column.
 AVG(): Returns the average value of a numeric column.
 MAX(): Returns the highest value.
 MIN(): Returns the lowest value.

Example (COUNT):

SELECT COUNT(*) FROM emp;

count

4
Example (MAX):

SELECT MAX(esal) FROM emp;

max(esal)

35000

Example (SUM):

SELECT SUM(esal) FROM emp;

sum(esal)

95000

Example (AVG):

SELECT AVG(esal) FROM emp;

avg(esal)

23750

2. STDDEV and VARIANCE:

 STDDEV(): Calculates the standard deviation.


 VARIANCE(): Calculates the variance.

Example:

SELECT STDDEV(esal) FROM emp;


stddev(esal)

8539.12564

You might also like