370 likes | 382 Views
Learn about grouping rows, using subqueries, and performing join operations in SQL. Discover how to group and count data, create tables and insert data using subqueries, and perform different types of joins.
E N D
INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.)
Group by • Group rows based on distinct values for columns • divides a table into groups of rows • used in • conjunction with the aggregate functions • to group the result-set by one or more columns. • Find out how many students are there in each sub.
Find out the number of students for each value of marks. select count(marks) as "no of students", marks from student group by marks;
Find out the number of employees working in each department • Employee Select count(dept_name) as No. of employees, dept_name from employee group by dept_name
HAVING CLAUSE • Used to restrict the group • It imposes a condition on group by clause which filters groups Find out students group having count more than 1 select count(marks) as "no of students",marks from student group by marks having count(marks)>1;
list the product names of literacy category that have winter_sales value more than 700 SELECT name, category, winter FROM salesWHERE category="Literary"GROUPBY winter HAVING winter > 700
Sub query (nested query) • A SQL statement that appear inside another SQL statement • Ex : - select … from ( select …from…..) • Syntax :- • Select column from table Where condition (select column from table Where condition) • Inner query executed first that returns certain rows • Next outer query is executed based on result of this inner query
SELECT category_name FROM categories WHERE category_id = ( SELECT MIN(category_id) from movies);
Employee (fname, lname, salary, dept_id) • Dept (dept_id, dept_name) • Find out employee detail working in finance department. • Select dept_id from dept where dept_name = ‘finance’ • Select fname,lname from employee where dept_id = • Select fname,lname from employee where dept_id = (Select dept_id from dept where dept_name = ‘finance’)
Creating a table using sub query • CREATE TABLE tablename AS SELECT-QUERY • This will create a new table and populate it with rows selected from other tables • CREATE TABLE temp AS Select eid,fname,lname, salary from employee WHERE dept_id =20; • Describe temp
Insert using sub query • Existing table can be populated with sub query • INSERT into TABLENAME (SELECT columnnames FROM tablename WHERE condition) • Ex :- INSERT INTO temp(eid, lname, fname) SELECT eid, lname, fname from EMPLOYEE WHERE dept_id=10; • Temp
Update using sub query • Updates can be performed using sub-query • Syntax :- UPDATE table-name • SET column-name = value • WHERE condition (SELECT SUBQUERY) • Ex:- update salary of employees using dept-id of finance department UPDATE employee SET salary = salary *1.10 WHERE dept_id = (SELECT dept_id FROM dept WHERE d_name = ‘Finance’)
Delete using sub-query • A row or rows from table can be deleted based on value returned by subquery • Syntax:- • DELETE FROM tablename WHERE condition (SELECT subquery) • Ex:- delete all employees in Accounting department using dept id. • DELETE FROM employee • WHERE dept_id = • (SELECT dept_id FROM department • where dept_name = ‘Accounting’);
Multiple row sub query • Multiple row sub query returns more than one row.
IN operator looks for at least one match from the list of values provided • Find out student details handled by computer dept. faculty. • Student • Faculty • select s_id, lastname, firstname, faculty_id from student where faculty_id in (select faculty_id from faculty where dept_name =‘computer’);
Ex:- IN • Identify items that are low (having quantity <20) in stock • Two Tables • Sales Order Items • Product • Select * From SalesOrderItems Where ProductID IN ( Select ID From Products Where Quantity < 20 )
ANY operator compare a value with any value in a list. • Ex:- • Find out the agent who worked for customer live in the city ‘AHMEDABAD'. • Agent • Customer SELECT ID,agent_name,working_area,commission FROM agents WHERE ID = ANY ( SELECT agent_id FROM customer WHERE city=‘Ahmedabad');
JOIN OPERATION • Working with multiple table • Manipulate data from all tables • Tables are joined on columns having same data type • combination of operations selection, projection and Cartesian product • Cross Join • Natural Join • Inner Join • Outer join • Full outer join • Left outer join • Right outer join
Cross join • Combines every row from left table to every row in right table • Not preferred :- may run for long time, • produces a huge result set that may not be useful CUSTOMERS ORDERS • Select id, name, amount, date from customers CROSS JOIN orders;
Natural Join • Most common type of join is a “natural join” (often just called “join”). R S conceptually is: • Compute R X S • The associated tables have one or more pairs of identically named columns • Select rows where attributes that appear in both relations have equal values • Select * FROM table1 NATURAL JOIN table2;
Natural Join • Select * from r1 NATURAL JOIN s1 R1 S1 R1 S1 =
Inner Join • creates a new result table • by combining column values of two tables (table1 and table2) • based upon the join-predicate. • SELECT table1.column1, table2.column2... FROM table1 INNER JOIN table2 ON condition;
Customer Order Select id, name, amount, date from customers inner join orders on customers.Id = orders.Customer_id;
Outer join • returns all rows from both the participating tables which satisfy the join condition and • Selects those tuples that donot satisfies the join condition • Left Outer Join On two relations R and S • It selects matching tuples of both relations R and S • Along with unmatched tuples from relation R • Missing values in relation s is set to NULL. • R S Selected records with matching values Records of R S R
SELECT * FROM table1 LEFT JOIN table2 ON table1.common_filed = table2.common_field; Employee Department Select * from employee LEFT JOIN Department ON employee.Dept_Name = Department.Dept_Name
Right Outer Join • JoinOn two relations R and S • It selects matching tuples of both relations R and S • Along with unmatched tuples from relation S • Missing values in relation R is set to NULL • R S Records of S Selected records with matching values S R
SELECT * FROM table1 RIGHT OUTER JOIN table2 ON table1.column_name=table2.column_name; Employee Department Select * from employee RIGHT JOIN Department ON employee.Dept_Name = Department.Dept_Name
Full Outer Join • JoinOn two relations R and S • It selects matching tuples of both relations R and S • Along with unmatched tuples from relation R and relation S • R S Records of S Selected records with matching values Records of R S R
Select * FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name; Employee Department Select * from employee FULL OUTER JOIN Department ON employee.Dept_Name = Department.Dept_Name
Semi join • Join on relation R and S contains tuples of R that participate in join operation of R and S • Only attributes of relation R is projected • Advantage • decreases the number of tuples to be handled to form join
Semi Join - EMP with PAY over the predicate EMP.TITLE = PAY.TITLE,