0% found this document useful (0 votes)
5 views

lecture 5

The document explains SQL subqueries, detailing their use with SELECT, INSERT, UPDATE, and DELETE statements. It also covers SQL INNER JOIN and CROSS JOIN operations, demonstrating how to combine data from multiple tables. Examples are provided for each type of query to illustrate their functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

lecture 5

The document explains SQL subqueries, detailing their use with SELECT, INSERT, UPDATE, and DELETE statements. It also covers SQL INNER JOIN and CROSS JOIN operations, demonstrating how to combine data from multiple tables. Examples are provided for each type of query to illustrate their functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL Subquery

 In SQL, a SELECT statement may contain another SQL


statement, known as a subquery or nested query.
 /*use a subquery to select the first name of customer with
the highest age*/

SELECT first_name, age


FROM customer
WHERE age= (
-- subquery
SELECT MAX(age)
FROM customer );
1
1. Subqueries with the Select
Statement
 Example: Select Customers with Minimum Age Using
Subquery
 -- select all the rows from the Customers table -- with the
minimum age

SELECT *
FROM customer
WHERE age = (
SELECT MIN(age)
FROM Customers
);
2
1. Subqueries with the Select
Statement
 SELECT *
FROM customer
where salary>=4500

 SELECT *
FROM customer
WHERE cid IN (SELECT cid FROM customer WHERE
SALARY >= 4500);

3
2. Subqueries with the INSERT
Statement
 SQL subquery can also be used with the Insert
statement. In the insert statement, data returned from
the subquery is used to insert into another table.

 INSERT INTO customer_copy


 SELECT *
 FROM customer
 WHERE cid IN (SELECT cid
 FROM customer);

4
3. Subqueries with the UPDATE
Statement
 The given example updates the SALARY by .25 times in
the customer table for all customers whose AGE is
greater than or equal to 29.

 UPDATE customer
 SET salary = salary * 0.25
 WHERE age IN (SELECT age FROM customer
 WHERE age >= 29);

5
4. Subqueries with the DELETE
Statement
 The given example deletes the records from the
customer table for all customer whose AGE is greater
than or equal to 29.

 DELETE FROM customer


 WHERE age IN (SELECT age FROM customer
 WHERE age >= 29 );

6
SQL INNER JOIN
 The SQL INNER JOIN statement joins two tables
based on a common column and selects rows that have
matching values in these columns.

 SQL INNER JOIN Syntax


 SELECT columns_from_both_tables
 FROM table1
 INNER JOIN table2
 ON table1.column1 = table2.column2

7
Example: Join Two Table Based on
Common Column
 -- join Customers and Orders tables based on -- cid of
customer and oid column of Orders

 SELECT cid, first_name, amount


 FROM customer as c SELECT cid, first_name, amount
FROM customer as c, orders as o
 inner join orders as o where c.cid = o.oid and
amount>=500;
 on c.cid = o.oid
 where amount>=500;

8
SQL CROSS JOIN
 In SQL, the CROSS JOIN operation allows us to
combine rows from two or more tables without any
specific relationship between them.

 Example
 SELECT *
 FROM student, teacher

9
Query to fill all columns at a time
 UPDATE student SET address = 'khulna‘ WHERE 1>0;

 ALTER TABLE student


ADD E_Address VARCHAR(30);

10

You might also like