0% found this document useful (0 votes)
21 views19 pages

Chapetr 07 SQL SubQueries

Uploaded by

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

Chapetr 07 SQL SubQueries

Uploaded by

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

Chapter Seven:

SubQueries
Outlines

Subqueries
SELECT Statement: Other
Clauses and Functions
Subquery.

A Subquery or Inner query or a Nested query is a


query within another SQL query and embedded
within the WHERE clause.
A subquery is used to return data that will be used
in the main query as a condition to further restrict the
data to be retrieved.
Subqueries can be used with the SELECT, INSERT,
UPDATE, and DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Subquery.

There are a few rules that subqueries must


follow −
1. Subqueries must be enclosed within parentheses.
2. A subquery can have only one column in the
SELECT clause.
3. An ORDER BY command cannot be used in a
subquery, although the main query can use an
ORDER BY.
4. The GROUP BY command can be used to
perform the same function as the ORDER BY in a
subquery.
Creating Table

 create table employees(id int, name varchar(20),


age int, addres varchar(20), salary int);
insert into employees values(1,'Ramesh',35,'Ahmed',200);
insert into employees values(2,'Khilan',25,'Delhi',150);
insert into employees values(3,'Kaushil',23,'Kolta',200);
insert into employees values(4,'Chaitali',25,'Mumbai',650);
insert into employees values(5,'Hardik',27,'Bhobal',850);
insert into employees values(6,'Komal',22,'MB',450);
insert into employees values(7,'Muffy',24,'Indore',100);
select * from employees;
Subqueries with the SELECT Statement

Subqueries are most frequently used with the SELECT statement. The basic syntax is as
follows :-
Example:7.1
SELECT *
FROM employees WHERE ID IN (SELECT ID
FROM employees
WHERE SALARY > 450) ;
Output:
Subqueries with insert
statement

Subqueries also can be used with INSERT


statements. The INSERT statement uses the data
returned from the subquery to insert into another
table. The selected data in the subquery can be
modified with any of the character, date or number
functions.
The basic syntax is as follows.
Consider a table employees1 with similar structure
as employees table. Now to copy the complete
employees table into the employees1 table, you can
use the following syntax.
Inserting Some Data

Creating Customers1 Table


create table employees1(id int, name varchar(20),
age int, addres varchar(20),
salary int);
<............Inserting Some Data Using Subqueries.....>
Example:7.2
INSERT INTO employees1
SELECT * FROM employees
WHERE ID IN (SELECT ID
FROM employees) ;
Select * from employees1;
Subqueries with the UPDATE
statement
Subqueries with the UPDATE Statement
The subquery can be used in conjunction with the UPDATE statement.
Either single or multiple columns in a table can be updated when using a
subquery with the UPDATE statement.
The basic syntax is as follows.
Example:7.3
Assuming, we have employees1 table available which is backup of
CUSTOMERS table. The following example updates name by ‘Kapoor’
times in the employees table for all the customers whose AGE is greater
than or equal to 27.
Subqueries with UPDATE
Statement
Example:7.3
UPDATE employees
SET NAME ='Kapoor'
WHERE AGE IN (SELECT AGE FROM employees1
WHERE AGE >= 27 );
The output will be like this it will effect two columns:
Output:
Subqueries with Delete
Statement
Subqueries with the DELETE Statement
The subquery can be used in conjunction with the DELETE statement like
with any other statements mentioned above.
The basic syntax is as follows.
Example
Assuming, we have a employees1 table available which is a backup of the
employees table. The following example deletes the records from the
employees table for all the customers whose AGE is greater than or equal
to 27.
Subquery.

Example:7.5
DELETE FROM employees WHERE AGE IN (SELECT AGE FROM
employees1 WHERE AGE >= 27 );
Output:
SELECT Statement: Other Clauses
and Functions.

Grouping Data
SELECT ...... FROM ...... WHERE condition ;
GROUP BY groupexpr [HAVING requirement]
The GROUP BY clause is used to combine, or group, rows with related
values to element of a smaller set of rows. GROUP BY is often used in
conjunction with SQL aggregate functions
GROUP BY Clause
The GROUP BY clause defines one or more columns as a group such
that all rows within any group have the same values for those columns.
EXAMPLE 6.26 Get all jobs of the employees:
USE sample
SELECT NAME, SUM(SALARY) FROM employees
GROUP BY NAME;
SELECT Statement: Other Clauses
and Functions.

Aggregate Functions
Aggregate functions are functions that are used to get summary values.
aggregate functionsThe
functions Transact-SQL language supports six aggregate
functions:
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( ) COUNT_BIG ()
SELECT Statement: Other Clauses
and Functions.

EXAMPLE 7.6
Get the lowest employees salary:
USE sample;
SELECT MIN(salary) AS min_Salary
FROM employees;
EXAMPLE : 7.7
Get the highest employee salary:
SELECT Max(salary) AS max_Salary
FROM employees;
SELECT Statement: Other Clauses
and Functions.

EXAMPLE 7.8
Calculate the sum of all employees salaries:
USE sample;
SELECT SUM (salary) sum_of_salaries
FROM employees;
EXAMPLE 7.9
Calculate the average of all salaries of the employees
USE sample;
SELECT AVG(salary) avg_salary
FROM employees;
SELECT Statement: Other Clauses
and Functions.

COUNT and COUNT_BIG Aggregate Functions The aggregate


function COUNT has two different forms:
COUNT ([DISTINCT] col_name) COUNT (*) The COUNT_BIG function
is similar to the COUNT function. The only Difference between them is in
relation to their return values: COUNT_BIG always Returns a value of the
BIGINT data type, while the COUNT function always returns a value of
the INTEGER data type
EXAMPLE 7.10 Count all differerent names in employees:
USE sample;
SELECT name, COUNT(distinct name) Name_count
FROM employees group by name;
EXAMPLE 7.11 Get the names of each employees:
USE sample;
SELECT name, COUNT(*) name_count FROM employees
SELECT Statement: Other Clauses
and Functions.

HAVING Clause.The HAVING clause defines the condition that is then


applied to groups of rows. The HAVING clause includes a comparison
establish .used to eliminate rows after the GROUP BY clause is applied to
the result set. Because it acts on the results of the GROUP BY clause,
aggregate functions can be used in the HAVING clause predicate.

EXAMPLE 7.12
Get id for all employes and their id should be < 1:
USE sample;
SELECT id
FROM employees
GROUP BY id
HAVING COUNT(*) < 1;
END

You might also like