1. Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423 603
(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NACC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Computer Engineering
(NBA Accredited)
Prof. Monika Agrawal
Assistant Professor
E-mail : agrawalmonikacomp@sanjivani.org.in
Contact No: 8770361037
Course:CO210
Database Management System
Lecture-03 SQL Clauses
2. Content
s
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 2
Select Clause
Where Clause
From Clause
Rename operation
Tuple Variable
4. Select clause
1. Retrieving Selected Fields/Columns
SELECT ID, NAME, SALARY FROM CUSTOMERS;
2. Retrieving All Fields/Columns
SELECT * FROM CUSTOMERS;
5. 3. DISTINCT Keyword on Single Columns
SELECT SALARY FROM CUSTOMERS ORDER BY SALARY;
SELECT DISTINCT SALARY FROM CUSTOMERS ORDER BY
SALARY;
4. DISTINCT Keyword on Multiple Columns
SELECT DISTINCT AGE, SALARY FROM CUSTOMERS
ORDER BY AGE;
5. Computing Using SELECT
SELECT NAME, SALARY*100 FROM CUSTOMER;
would return a relation that is the same as the customer relation,
except that the value of the attribute salary is multiplied by 100.
6. Where Clause
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 6
• The where clause specifies conditions that the result must satisfy
• Corresponds to the selection predicate of the relational algebra.
1. SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000;
WHERE Clause with Update Statement
2. UPDATE CUSTOMERS set SALARY = SALARY+10000 where NAME = 'Ramesh’;
WHERE Clause with IN Operator- Using the IN operator you can specify the list of values or
sub query in the where clause.
3. SELECT * from CUSTOMERS WHERE NAME IN ('Khilan', 'Hardik', 'Muffy');
7. WHERE Clause with NOT IN Operator
4. SELECT * from CUSTOMERS WHERE AGE NOT IN (25,
23, 22);
WHERE Clause with LIKE Operator- The WHERE clause
with LIKE operator allows us to filter rows that matches
a specific pattern. This specific pattern is represented by
wildcards (such as %, _, [] etc).
5. SELECT * FROM CUSTOMERS WHERE NAME LIKE 'K
%’;
WHERE Clause with AND, OR Operators
6. SELECT * FROM CUSTOMERS WHERE (AGE = 25 OR salary <
4500) AND (name = 'Komal' OR name ='Kaushik');
8. Group By Clause- we group the record of a table to perform
calculations on them. Therefore, the SQL GROUP BY clause is often
used with the aggregate functions such as SUM(), AVG(), MIN(), MAX(),
COUNT(), etc
7. SELECT AGE, COUNT(Name) FROM CUSTOMERS GROUP BY AGE;
8. SELECT AGE, MAX(salary) AS MAX_SALARY FROM CUSTOMERS
GROUP BY AGE;
9. SELECT AGE, MIN(SALARY) AS MIN_SALARY FROM CUSTOMERS
GROUP BY AGE ORDER BY MIN_SALARY DESC;
9. GROUP BY with HAVING Clause
We can also use the GROUP BY clause with the HAVING clause
filter the grouped data in a table based on specific criteria.
Syntax-
SELECT column1, column2, aggregate_function(column)
FROM table_name
GROUP BY column1, column2
HAVING condition;
SELECT ADDRESS, AGE, MIN(SALARY) AS MIN_SUM
FROM CUSTOMERS
GROUP BY ADDRESS, AGE HAVING AGE>24;
10. Aliasing Column Names
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 8
Syntrax- SELECT column_name AS alias_name FROM table_name;
SELECT ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME FROM
CUSTOMERS;
11. Table Aliasing: Customer and Order Table
SELECT C.ID, C.NAME, C.AGE, O.AMOUNT FROM CUSTOMERS AS C, ORDERS AS O
WHERE C.ID = O.CUSTOMER_ID;
12. Question & Answer
Query
Example 1: Find all professors whose salary is greater than the average budget of
all the departments.
Instructor relation: Department Relation
13. Solution
Select I.ID, I.NAME, I.DEPARTMENT, I.SALARY from
(select avg(BUDGET) as averageBudget from DEPARTMENT) as
BUDGET, Instructor as I
where I.SALARY > BUDGET.averageBudget;
Explanation: The average budget of all departments from the department relation is 70000.
Erik and Smith are the only instructors in the instructor relation whose salary is more than
70000 and therefore are present in the output relation.