Queries Using Group by
Queries Using Group by
Week - 5(a)
(Queries using Group By)
Solution:
1. Know the purpose of GROUP BY Keyword in SQL.
2. Use the GROUP BY Keyword to divide rows in a table into s maller groups
GROUP BY Clause:
Q) Find the total amount of money spent for each department in employees table
based on salary.
7 rows selected.
Solution:
1. Learn HAVING Keyword in SQL.
2. Use the HAVING clause to restrict groups
3. When you use the HAVING clause, the Oracle server restricts groups as follows:
i. Rows are grouped.
ii. The group function is applied.
iii. Groups matching the HAVING clause are displayed.
HAVING Clause:
Having Clause is used in SQL to filter a group of rows especially to filter with
aggregate operators.
ORDER BY Keyword :
The ORDER BY clause can be used to sort the rows. Use the ORDER BY clause to
display the rows in a specific order(Ascending order, descending order).
- ASC orders the rows in ascending order (this is the default order)
- DESC orders the rows in descending order
The ORDER BY clause comes last in the SELECT statement
Syntax
SELECT expr
FROM table
WHERE condition(s)
ORDER BY {column/ expr } [ASC|DESC];
EXAMPLE FOR ORDER BY Keyword
Q) Find the employee-id along with his first name and last name with highest
salary displayed first in descending order.
SQL > SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,SALARY
FROM EMPLOYEES
ORDER BY SALARY DESC
Q) Find the department_id whose average salary is less than 6500 with highest
average salary displayed first in descending order.
Week - 6(a)
(Queries using Commit)
Proble m State ment:
Usage of TCL(Transaction Control Language) Commands -COMMIT Keyword in SQL.
Solution:
1. Learn TCL(Transaction Control Language) Commands in SQL.
2. Know the purpose of COMMIT Keyword in a database.
TCL (Transaction Control Language) Keywords:
Commit
Rollback
Savepoint
Commit:
Commit keyword saves the data permanently in the database. If commit state ment is not issued
after Insert keyword ,then the inserted rows or DML Statements re mains the in the database till
the user logs-in the database, Once the user logs out his session then the data inserted will be
erased off from the database.
Solution:
1. Learn TCL(Transaction Control Language) Commands in SQL.
2. Know the purpose of SAVEPOINT Keyword in a database.
Savepoint:
Savepoint rollbacks the database state to a Specific Point (Savepoint) that has
been already defined with Savepoint. We can define any number of save
points in a database.
OVEN','SAMSUNG');
WEEK -7
(Queries on Joins and Sub-Queries)
Week - 7(a)
(sub queries)
Solution:
ROLL_NO STUD_NAME MARKS1 MARKS2 MARKS3 MARKS4 MARKS5 TOTAL PERCENT AGE
105 Vishnu 99 98 99 100 94 490 98 22
104 Swathi 96 98 97 94 91 476 95.2 22
107 Karuna 96 98 89 90 94 467 93.4 21
103 Raju 92 94 96 92 89 463 92.6 21
102 Krishna 89 81 91 93 78 419 83.8 21
101 Kiran 77 68 78 91 87 401 80.2 19
100 ravi 73 84 56 69 90 372 74.4 19
106 Reshma 76 81 59 70 74 360 72 20
8 rows selected.
Sub Queries Examples
Q) Select the roll number and name of the student who secured fourth rank
in the class.
EMPLOY FIRST_ LAST_ EM PHONE_N HIRE_ JOB_ SAL COMMISSI MANAG DEPARTM
EE_I D NAME NAME AIL UMBER DATE ID ARY ON_P CT ER_ID ENT_ID
SKI 515.123.45 17- AD_P 2400
100 Steven King 90
NG 67 JUN-87 RES 0
Q) Find all the employee details who salary is above Abel’s salary.
SQL > select * from e mployees where salary >
EMPLO FIRST_ LAST_ PHONE_N HIRE_ JOB_ SAL COMMISSI MANAG DEPARTM
EMAIL
YEE_ID NAME NAME UMBER DATE ID ARY ON_P CT ER_ID ENT_ID
17-
515.123.45 AD_P 2400
100 Steven King SKING JUN- 90
67 RES 0
87
21-
Kochha NKOC 515.123.45 AD_V 1700
101 Neena SEP- 100 90
r HHA R 68 P 0
89
13-
De LDE HA 515.123.45 AD_V 1700
102 Lex JAN- 100 90
Haan AN 69 P 0
93
17-
Greenb NGRE 515.124.45 FI_M 1200
108 Nancy AUG- 101 100
erg ENBE 69 GR 0
94
01-
JRUSS 011.44.134 SA_M 1400
145 John Russell OCT- .4 100 80
EL 4.429268 AN 0
96
10 rows selected.
EMPLOY FIRST_ LAST_ PHONE_N HIRE_ JOB SAL COMMISSI MANAG DEPARTM
EMAIL
EE_I D NAME NAME UMBER DATE _ID ARY ON_P CT ER_ID ENT_ID
21-
Kochha NKOC 515.123.45 AD_ 1700
101 Neena SEP- 100 90
r HHA R 68 VP 0
89
De LDE HA 515.123.45 13- AD_ 1700
102 Lex 100 90
Haan AN 69 JAN-93 VP 0
Solution:
• Join Definition:
A join is an SQL operation which will combine or join two or more tables to retrieve data or
information from more than one table.
A Cartesian product also known as cross join is an SQL operation that combines 2 relations
(tables) which produces a new relation by taking all the rows values from one table (first table)
and joins with every member (all rows) of another relation(table second).
1 row created.
1 row created.
A Cross join is defined in SQL by specifying the table names participating in the cross join after
Solution:
Equi Join
When we have to join 2 tables with equality operator , the common values of attributes for the
corresponding tables are taken into conside ration and if they are matched then they are included
in the join operation
Solution:
Outer Joins
• Consider two relations R & S ,Equi join or inner join is performed when tuples from R have
matching tuples in S or vice versa.
• A join is said to be an outer join, if we want to keep all the rows eithe r in R or in S or in both
R and S regardless of whether or not they have matching tuples in both relations.
• When we pe rform outer join if there are no matching rows values in outer table or other table,
they are replaced as null values.
• Left–Outer join takes all the tuples or rows present in the left relation or table and joins with
tuples present in right table, if there is no matching tuple in right relation then adds or
places null values for all columns in right table.