SlideShare a Scribd company logo
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
Content
s
DEPARTMENT OF COMPUTER ENGINEERING, Sanjivani COE, Kopargaon 2
Select Clause
Where Clause
From Clause
Rename operation
Tuple Variable
Customer
Select clause
1. Retrieving Selected Fields/Columns
SELECT ID, NAME, SALARY FROM CUSTOMERS;
2. Retrieving All Fields/Columns
SELECT * FROM CUSTOMERS;
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.
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');
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');
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;
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;
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;
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;
Question & Answer
Query
Example 1: Find all professors whose salary is greater than the average budget of
all the departments.
Instructor relation: Department Relation
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.

More Related Content

PPTX
Sql clauses by Manan Pasricha
PPTX
Basic SQL commands and explanation .pptx
PPTX
PPTX
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
PPTX
Lec 07 SQL - 1.pptx
PPT
UNIT 2 Structured query language commands
PPTX
Server Query Language – Getting Started.pptx
PPTX
WEEK-2 DML and operators power point presentation
Sql clauses by Manan Pasricha
Basic SQL commands and explanation .pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Lec 07 SQL - 1.pptx
UNIT 2 Structured query language commands
Server Query Language – Getting Started.pptx
WEEK-2 DML and operators power point presentation

Similar to Basic SQL Queries with Clauses Learning.pptx (20)

PPTX
PPT
SQL : introduction
PPT
SQL || overview and detailed information about Sql
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PPTX
DB2 Sql Query
PPTX
XII_IP_PPT[2].pptxhggggggggggggggggggggggggggggggggdj
PDF
Sql where clause
PDF
Sql where clause
PPTX
SQL Data Manipulation
PDF
best way of expla DBMS Clause in SQL.pdf
RTF
Best sql plsql_material for B.TECH
PPTX
Structured Query Language (SQL) Part 2.pptx
PPTX
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
PPT
Sql query [select, sub] 4
PPTX
SQL data types: INT, VARCHAR, CHAR,.pptx
PPTX
SQL Fundamentals
PPT
sql_data_manipulation.ppt
DOCX
Assg2 b 19121033-converted
PPTX
Introduction to SQL
PPTX
SQL Operators.pptx
SQL : introduction
SQL || overview and detailed information about Sql
Database Systems - SQL - DDL Statements (Chapter 3/3)
DB2 Sql Query
XII_IP_PPT[2].pptxhggggggggggggggggggggggggggggggggdj
Sql where clause
Sql where clause
SQL Data Manipulation
best way of expla DBMS Clause in SQL.pdf
Best sql plsql_material for B.TECH
Structured Query Language (SQL) Part 2.pptx
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
Sql query [select, sub] 4
SQL data types: INT, VARCHAR, CHAR,.pptx
SQL Fundamentals
sql_data_manipulation.ppt
Assg2 b 19121033-converted
Introduction to SQL
SQL Operators.pptx
Ad

More from agrawalmonikacomp (20)

PPT
Introduction to MongoDB CRUD Operations.ppt
PPT
Lecture 2 Difference between NoSQL and SQL.ppt
PPTX
Lecture 1-Introduction of NoSQL in DBMS.pptx
PPT
Lecture 6 Failure Classification in DBMS.ppt
PPT
Lecture 5 Deadlocks in Database Systems.ppt
PPT
Lecture 1-Introduction to Database Transactions.ppt
PPTX
Learning of 3NF BCNF Normal Forms in DBMS.pptx
PPTX
First Normal Form, Second Normal Form.pptx
PPTX
Learning of L2 Codd's Rules 7-12 in DBMS.pptx
PPTX
Learning of Codd's Rules 0-6 in DBMS.pptx
PPTX
Learning of PL-SQL Introduction in DBMS.pptx
PPTX
SQL Indexes Creation and Use in DBMS.pptx
PPTX
SQL Aggregate functions Learning in DBMS.pptx
PPTX
Data Definition Language Commands in DBMS
PPTX
Introduction to Structured Query Language
PPTX
Weak Entity Sets in Database Management System.pptx
PPTX
Learning Cardinalities and Relationships.pptx
PPTX
Entity Relationship Model in Database Systems
PPTX
DBMS Languages in Database Management System
PPTX
Data Models in Database Management System
Introduction to MongoDB CRUD Operations.ppt
Lecture 2 Difference between NoSQL and SQL.ppt
Lecture 1-Introduction of NoSQL in DBMS.pptx
Lecture 6 Failure Classification in DBMS.ppt
Lecture 5 Deadlocks in Database Systems.ppt
Lecture 1-Introduction to Database Transactions.ppt
Learning of 3NF BCNF Normal Forms in DBMS.pptx
First Normal Form, Second Normal Form.pptx
Learning of L2 Codd's Rules 7-12 in DBMS.pptx
Learning of Codd's Rules 0-6 in DBMS.pptx
Learning of PL-SQL Introduction in DBMS.pptx
SQL Indexes Creation and Use in DBMS.pptx
SQL Aggregate functions Learning in DBMS.pptx
Data Definition Language Commands in DBMS
Introduction to Structured Query Language
Weak Entity Sets in Database Management System.pptx
Learning Cardinalities and Relationships.pptx
Entity Relationship Model in Database Systems
DBMS Languages in Database Management System
Data Models in Database Management System
Ad

Recently uploaded (20)

PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
RMMM.pdf make it easy to upload and study
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
master seminar digital applications in india
PDF
01-Introduction-to-Information-Management.pdf
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
RMMM.pdf make it easy to upload and study
UNIT III MENTAL HEALTH NURSING ASSESSMENT
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial disease of the cardiovascular and lymphatic systems
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
LDMMIA Reiki Yoga Finals Review Spring Summer
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Cell Types and Its function , kingdom of life
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
master seminar digital applications in india
01-Introduction-to-Information-Management.pdf
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
History, Philosophy and sociology of education (1).pptx
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx

Basic SQL Queries with Clauses Learning.pptx

  • 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.