Clause DBMS Yshword
Clause DBMS Yshword
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).
SELECT
DELETE
UPDATE
Syntax:
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:
Output:
3 Rohit 264
2. If we want to show only the name of the player with rank 3, the query will be:
Output:
Name
Rohit
UPDATE:
To update records in a table, use the UPDATE command. The syntax is:
Example:
Output:
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:
Example:
HAVING Clause:
Example:
If we want to know how many departments there are in the table, without using the HAVING
clause, we write:
Output:
Dname
Sales
HR
Product
Dname Count(*)
Sales 6
HR 3
Product 2
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:
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
10 Sales Delhi
20 HR Goa
30 Product Mumbai
Table 2: emp
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:
Work Dept Id
Teacher BCA 20
Doctor MBBS 40
Teacher MBA 50
Programmer JAVA 60
Cashier ICICI 10
EQUI JOIN:
Example:
SQL> SELECT name, address, work FROM personal, professional WHERE personal.id
= professional.id;
Output:
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:
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:
Cross Join: Used to combine every record from the first table with every record from the second
table, resulting in a Cartesian product.
Example:
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:
SQL Operators
1. Arithmetic Operators:
*: Multiplication
+: Addition
-: Subtraction
/: Division
3. Logical Operators:
Example: To display employees with salary 20000 and joining date '15-Aug-2000':
Set operators allow operations between two or more tables, producing results based on the
chosen operator.
Example (UNION):
Example (INTERSECT):
Sroll Sname
30 Akhilesh
SQL Functions
1. Aggregate Functions:
Example (COUNT):
count
4
Example (MAX):
max(esal)
35000
Example (SUM):
sum(esal)
95000
Example (AVG):
avg(esal)
23750
Example:
8539.12564