CSC CHP 13
CSC CHP 13
What is the difference between HAVING and WHERE clause ? group of fields. This grouping results in one summary record per group if group functions, such as aggregate
functions, are used with it.
Answer
Question 3
HAVING clause WHERE clause What are aggregate functions? What is their use? Give some examples.
HAVING conditions are applicable to WHERE conditions are Answer
groups formed by GROUP BY clause. applicable to individual rows. Aggregate functions in SQL work with data from multiple rows at a time and return a single aggregated
value. They are used to perform calculations across multiple rows and return a summary result for that
HAVING conditions can include WHERE conditions cannot
group.
aggregate functions. include aggregate functions.
Examples of aggregate functions include SUM(), COUNT(), MAX(), MIN(), AVG() etc.
It allows conditions to be applied to It filters rows based on Question 4
grouped data. specified conditions.
What type of functions can you use with GROUP BY and HAVING clauses ?
Question 2
Answer
What is the use of GROUP BY clause ?
Aggregate functions are used with the GROUP BY and HAVING clauses to apply conditions on grouped data.
Answer
Question 5
What is sql join ? How is it useful ?
Answer BOOK_ID
A SQL join is a query that combines rows from two or more tables. Join allows retrieving data from related
tables simultaneously in a single query. Joins enable the combination of data from different tables to obtain BOOK_TITLE
a comprehensive view of the information.
PRICE
Question 6
Which SQL statement allows you to find the highest price from the table BOOK_INFORMATION?
What are most common types of SQL joins ?
1. SELECT BOOK_ID, BOOK_TITLE, MAX(PRICE) FROM BOOK_INFORMATION;
Answer
2. SELECT MAX(PRICE) FROM BOOK_INFORMATION;
The most common types of SQL joins are as follows :
3. SELECT MAXIMUM(PRICE) FROM BOOK_INFORMATION;
1. Equi-Join
4. SELECT PRICE FROM BOOK_INFORMATION ORDER BY PRICE DESC;
2. Non-Equi-Join
Answer
3. Natural join
SELECT MAX(PRICE) FROM BOOK_INFORMATION;
Type B: Application Based Questions
Explanation
Question 1
1. SELECT BOOK_ID, BOOK_TITLE, MAX(PRICE) FROM BOOK_INFORMATION; — This query selects the
Table BOOK_INFORMATION
BOOK_ID, BOOK_TITLE, and maximum PRICE from the BOOK_INFORMATION table. However, the
Column Name requirement is to find the highest price only.
2. SELECT MAX(PRICE) FROM BOOK_INFORMATION; — This query selects the maximum PRICE from the 1. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES;
BOOK_INFORMATION table using the MAX() aggregate function. This option is correct because it 2. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES ORDER BY STORE_ID;
directly retrieves the highest price among all the books listed in the BOOK_INFORMATION table,
which is what the question asks for. 3. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID;
3. SELECT MAXIMUM(PRICE) FROM BOOK_INFORMATION; — There is no MAXIMUM() function in SQL. 4. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES HAVING UNIQUE STORE_ID;
4. SELECT PRICE FROM BOOK_INFORMATION ORDER BY PRICE DESC; — This query selects all prices from Answer
the BOOK_INFORMATION table and orders them in descending order using ORDER BY PRICE DESC but SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID;
it doesn't directly give the highest price.
Explanation
Question 2
1. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES; — This statement selects the store_ID and
Table SALES calculates the total sales amount using SUM() aggregate function from the SALES table. It does not
group the results by store ID, so it will return a single row with the total sales amount across all stores.
Column Name
2. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES ORDER BY STORE_ID; — This statement selects
STORE_ID the store_ID and calculates the total sales amount using SUM() aggregate function from the SALES
table and uses an ORDER BY clause to sort the results by store ID. However, it still doesn't group the
SALES_DATE results by store_ID.
SALES_AMOUNT 3. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID; — This statement
selects the store_ID and calculates the total sales amount using SUM() aggregate function from the
Which SQL statement lets you find the sales amount for each store? SALES table and uses the GROUP BY clause to group the results by store ID. It calculates the total sales
amount for each store ID separately. As a result, it calculates the total sales amount for each unique 2. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING SALES_AMOUNT
store ID separately, providing a breakdown of sales amounts for each store in the dataset. > 5000;
4. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES HAVING UNIQUE STORE_ID; — This statement 3. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
is incorrect because the HAVING clause is used for filtering grouped data based on a condition, not for WHERE SUM(SALES_AMOUNT) > 5000 GROUP BY STORE_ID;
identifying unique values. Also, "UNIQUE STORE_ID" is not a valid condition in SQL.
4. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
Question 3 WHERE SALES_AMOUNT > 5000 GROUP BY STORE_ID;
Table SALES Answer
3. SELECT DISTINCT STORE_ID FROM SALES; — This option selects distinct (unique) store IDs from the 2. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
SALES table but does not count or provide the total number of stores. GROUP BY STORE_ID HAVING STORE_ID IN (25, 45);
4. SELECT COUNT(STORE_ID) FROM SALES GROUP BY STORE_ID; — This option attempts to count the 3. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE STORE_ID IN (25, 45);
number of occurrences of each store ID by using COUNT(STORE_ID) and grouping by store ID with 4. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
GROUP BY STORE_ID. However, this results in a count for each unique store ID separately, not the
WHERE STORE_ID = 25 AND STORE_ID = 45 GROUP BY STORE_ID;
total number of stores in the table.
Answer
Question 5
SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
Table SALES
WHERE STORE_ID IN (25, 45) GROUP BY STORE_ID;
Column Name
Explanation
STORE_ID 1. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE STORE_ID IN (25, 45) GROUP BY
STORE_ID; — This query uses the IN operator to filter rows where the STORE_ID is either 25 or 45. It
SALES_DATE then calculates the total sales amount for each store ID using SUM(SALES_AMOUNT) and groups the
results by STORE_ID. This query correctly finds the total sales amount for Store ID 25 and Store ID 45
SALES_AMOUNT separately.
Which SQL statement allows you to find the total sales amount for Store ID 25 and the total sales amount 2. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING STORE_ID IN
for Store ID 45? (25, 45); — This query will also give the required output but it is inefficient because it first retrieves all
1. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES rows from the "SALES" table, then groups the results by store ID, and finally filters the result set to
WHERE STORE_ID IN (25, 45) GROUP BY STORE_ID; include only store IDs 25 and 45. The inefficiency arises from the fact that it processes all rows in the
"SALES" table before filtering out the unnecessary data. This means that it processes more data than STU ID FNAME LNAME EXAM ID EXAM_SCORE
necessary, which can be wasteful in terms of computational resources and time. A more efficient
approach would be to select only the rows having store IDs 25 and 45 first (using WHERE clause), and 10 LAURA LYNCH 2 85
then perform the aggregation.
11 GRACE BROWN 1 78
3. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE STORE_ID IN (25, 45); — This query
filters rows where the STORE_ID is either 25 or 45 and calculates the total sales amount for these
11 GRACE BROWN 2 72
store IDs using SUM(SALES_AMOUNT). However, it doesn't include a GROUP BY clause, so it would
return a single row with the total sales amount for both Store ID 25 and Store ID 45 combined. 12 JAY JACKSON 1 95
4. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE STORE_ID = 25 AND STORE_ID = 45
GROUP BY STORE_ID; — This query filter rows where the STORE_ID is both 25 and 45 simultaneously 12 JAY JACKSON 2 92
using STORE_ID = 25 AND STORE_ID = 45. However, this condition is impossible to satisfy because a
13 WILLIAM BISHOP 1 70
single value cannot be both 25 and 45 at the same time. Therefore, this query would not return any
results.
13 WILLIAM BISHOP 2 100
Question 6
14 CHARLES PRADA 2 85
Table EXAM_RESULTS
What SQL statement do we use to find the average exam score for EXAM_ID = 1?
STU ID FNAME LNAME EXAM ID EXAM_SCORE
1. SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS;
10 LAURA LYNCH 1 90
2. SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID WHERE EXAM_ID = 1;
3. SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID HAVING EXAM_ID = 1; Additionally, grouping by EXAM_ID and then trying to filter by EXAM_ID = 1 within the GROUP BY
4. SELECT COUNT(EXAM_SCORE) FROM EXAM_RESULTS WHERE EXAM_ID = 1; clause will result in an error because grouping should be done before filtering.
3. SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID HAVING EXAM_ID = 1; — This
Answer
query groups the exam results by EXAM_ID and then calculates the average exam score for each
SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID HAVING EXAM_ID = 1; group. The HAVING clause filters the groups and returns only those where the EXAM_ID is equal to 1,
Output giving us the average exam score for the exam with EXAM_ID equal to 1.
2. SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID WHERE EXAM_ID = 1; — This 11 GRACE BROWN 1 78
statement is incorrect because the WHERE clause should come before the GROUP BY clause.
11 GRACE BROWN 2 72
STU ID FNAME LNAME EXAM ID EXAM_SCORE Output
+---------+------------------------+
12 JAY JACKSON 1 95
| EXAM_ID | COUNT(DISTINCT STU_ID) |
12 JAY JACKSON 2 92 +---------+------------------------+
13 WILLIAM BISHOP 1 70 | 1| 4|
| 2| 5|
13 WILLIAM BISHOP 2 100
+---------+------------------------+
14 CHARLES PRADA 2 85 Explanation
Which SQL statement do we use to find out how many students took each exam? 1. SELECT COUNT(DISTINCT STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; — It groups the
EXAM_RESULTS table by EXAM_ID and uses the COUNT(DISTINCT STU_ID) function to count the
1. SELECT COUNT(DISTINCT STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID;
number of distinct student IDs for each exam. However, the result set does not include EXAM_ID.
2. SELECT EXAM_ID, MAX(STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID;
2. SELECT EXAM_ID, MAX(STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; — This query groups the
3. SELECT EXAM_ID, COUNT(DISTINCT STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; results by EXAM_ID and then selects the maximum STU_ID for each exam. However, this doesn't
4. SELECT EXAM_ID, MIN(STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; provide the count of students who took each exam, as it gives the maximum student ID instead of
counting the distinct student IDs.
Answer
3. SELECT EXAM_ID, COUNT(DISTINCT STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; — It groups
SELECT EXAM_ID, COUNT(DISTINCT STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID;
the EXAM_RESULTS table by EXAM_ID and uses the COUNT(DISTINCT STU_ID) function to count the
number of distinct student IDs for each exam. The result set includes the EXAM_ID and the count of STU ID FNAME LNAME EXAM ID EXAM_SCORE
students who took each exam.
4. SELECT EXAM_ID, MIN(STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; — This query groups the 12 JAY JACKSON 2 92
results by EXAM_ID and selects the minimum STU_ID for each exam. It does not provide information
13 WILLIAM BISHOP 1 70
about the number of students who took each exam.
Question 8 13 WILLIAM BISHOP 2 100
Table EXAM_RESULTS
14 CHARLES PRADA 2 85
STU ID FNAME LNAME EXAM ID EXAM_SCORE
What SQL statement do we use to print out the record of all students whose last name starts with 'L'?
10 LAURA LYNCH 1 90 1. SELECT * FROM EXAM_RESULTS WHERE LNAME LIKE 'L%' ;
2. SELECT * FROM EXAM_RESULTS WHERE LNAME LIKE 'L';
10 LAURA LYNCH 2 85
3. SELECT * FROM EXAM_RESULTS WHERE LNAME 'L';
11 GRACE BROWN 1 78
4. SELECT * FROM EXAM_RESULTS WHERE LNAME <> 'L';
11 GRACE BROWN 2 72 Answer
SELECT * FROM EXAM_RESULTS WHERE LNAME LIKE 'L%' ;
12 JAY JACKSON 1 95
Output
+--------+-------+-------+---------+------------+
| stu_id | fname | lname | exam_id | exam_score | 4. SELECT * FROM EXAM_RESULTS WHERE LNAME <> 'L'; — This query retrieves records where the last
+--------+-------+-------+---------+------------+ name is not equal to 'L'. It does not specifically look for last names starting with 'L', so it's not the
correct option for the given requirement.
| 10 | LAURA | LYNCH | 1| 90 |
Question 9
| 10 | LAURA | LYNCH | 2| 85 |
Table EXAM_RESULTS
+--------+-------+-------+---------+------------+
STU ID FNAME LNAME EXAM ID EXAM_SCORE
Explanation
1. SELECT * FROM EXAM_RESULTS WHERE LNAME LIKE 'L%'; — The LIKE operator is used for pattern 10 LAURA LYNCH 1 90
matching in SQL. '%' is a wildcard character that matches zero or more characters. 'L%' specifies that
the last name (LNAME) should start with 'L' followed by zero or more characters. The SELECT 10 LAURA LYNCH 2 85
* statement retrieves all columns from the EXAM_RESULTS table for the matching records.
11 GRACE BROWN 1 78
2. SELECT * FROM EXAM_RESULTS WHERE LNAME LIKE 'L'; — This query attempts to select all columns
(*) from the EXAM_RESULTS table where the last name (LNAME) is exactly equal to 'L'. However, 11 GRACE BROWN 2 72
when using the LIKE operator in SQL for pattern matching, we use wildcard characters (%) to
represent unknown parts of a string. 12 JAY JACKSON 1 95
3. SELECT * FROM EXAM_RESULTS WHERE LNAME 'L'; — This statement contains a syntax error. In SQL,
12 JAY JACKSON 2 92
when using the WHERE clause to filter records based on a specific condition, we need to use
comparison operators or functions to define the condition properly. 13 WILLIAM BISHOP 1 70
+----------+ The SQL query SELECT AVG(Pay) FROM CLUB WHERE Sports = 'KARATE' ; calculates the average value of the
| 34 | 'Pay' column from the 'CLUB' table where the 'Sports' column has the value 'KARATE'. This query helps us to
get information about the average pay of karate coaches in the club.
+----------+
4.
Explanation
Output
The SQL query SELECT MIN(Age) FROM CLUB WHERE Sex = 'F' ; retrieves the minimum Age from the 'CLUB'
+----------+
table where the 'Sex' column has the value 'F'. This query gives us the age of the youngest female coach in
the club. | SUM(Pay) |
3. +----------+
Output | 7800 |
+-----------+ +----------+
| AVG(Pay) | Explanation
+-----------+ The SQL query SELECT SUM(Pay) FROM CLUB WHERE Dateofapp > '1998-01-31'; calculates the sum of the
| 1100.0000 | 'Pay' column from the 'CLUB' table where the 'Dateofapp' column has a date value greater than '1998-01-
31'. This query gives us the total pay of all the coaches who joined after 31/01/1998.
+-----------+
Question 11
Explanation
In a Database, there are two tables given below :
Table : EMPLOYEE JOBID JOBTITLE SALARY
EMPLOYEEID NAME SALES JOBID
104 Accounting Manager 70000
E1 SUMIT SINHA 1100000 102
105 Accountant 65000
E2 VIJAY SINGH TOMAR 1300000 101
106 Sales Manager 80000
E3 AJAY RAJPAL 1400000 103 Write SQL Queries for the following :
E4 MOHIT RAMNANI 1250000 102 1. To display employee ids, names of employees, job ids with corresponding job titles.
2. To display names of employees, sales and corresponding job titles who have achieved sales more than
E5 SHAILJA SINGH 1450000 103 1300000.
Table : JOB 3. To display names and corresponding job titles of those employees who have 'SINGH' (anywhere) in
their names.
JOBID JOBTITLE SALARY
4. Identify foreign key in the table EMPLOYEE.
101 President 200000 5. Write SQL command to change the JOBID to 104 of the EMPLOYEE with ID as E4 in the table
'EMPLOYEE'.
102 Vice President 125000
Answer
103 Administration Assistant 80000
1.
Eid Name Depid Qualification Sec Eid Basic D.A HRA Bonus
5 Sumit Mittal 103 B.Tech. M 1. To display the frequency of employees department wise.
2. To list the names of those employees only whose name starts with 'H'
6 Jyoti 101 M.Tech. F
3. To add a new column in salary table. The column name is Total_Sal.
Table : Salary
4. To store the corresponding values in the Total_Sal column.
Eid Basic D.A HRA Bonus 5. Select max(Basic) from Salary where Bonus > 40 ;
1 6000 2000 2300 200 6. Select count(*) from Employee group by Sex ;
7. Select Distinct Depid from Employee ;
2 2000 300 300 30
Answer
3 1000 300 300 40 1.
SELECT Depid, COUNT(*) AS Frequency
FROM Employee WHERE Bonus > 40;
GROUP BY Depid; Output
2. +------------+
SELECT Name | MAX(Basic) |
FROM Employee +------------+
WHERE Name LIKE 'H%'; | 10000 |
3. +------------+
ALTER TABLE Salary 6.
ADD COLUMN Total_Sal FLOAT; SELECT Sec as sex, COUNT(*) AS Count
4. FROM Employee
UPDATE Salary GROUP BY Sec ;
SET Total_Sal = Basic + `D.A.` + HRA + Bonus; Output
5. +-----+-------+
SELECT MAX(Basic) | sex | Count |
FROM Salary +-----+-------+
|F | 2| Create following tables such that Empno and Sno are not null and unique, date of birth is after '12-Jan-
|M | 4| 1960', name is never blank, Area and Native place is valid, hobby, dept is not empty, salary is between 4000
and 10000.
+-----+-------+
Table : Personal
7.
Empno Name Dobirth Native-place Hobby
SELECT DISTINCT Depid FROM Employee ;
Output 123 Amit 23-Jan-1965 Delhi Music
+-------+
127 Manoj 12-dec-1976 Mumbai Writing
| Depid |
124 Abhai 11-aug-1975 Allahabad Music
+-------+
| 101 | 125 Vinod 04-apr-1977 Delhi Sports
| 102 |
128 Abhay 10-mar-1974 Mumbai Gardening
| 103 |
129 Ramesh 28-oct-1981 Pune Sports
+-------+
Question 13 Table : Job
With reference to following relations PERSONAL and JOB answer the questions that follow :
Sno Area App_date Salary Retd_date Dept (g) Show the appointment date and native place of those whose name starts with 'A' or ends in 'd'.
(h) Show the salary expense with suitable column heading of those who shall retire after 20-jan-2006.
123 Agra 25-jan-2006 5000 25-jan-2026 Marketing
(i) Show additional burden on the company in case salary of employees having hobby as sports, is increased
127 Mathura 22-dec-2006 6000 22-dec-2026 Finance by 10%.
(j) Show the hobby of which there are 2 or more employees.
124 Agra 19-aug-2007 5500 19-aug-2027 Marketing
(k) Show how many employee shall retire today if maximum length of service is 20 years.
125 Delhi 14-apr-2004 8500 14-apr-2018 Sales (l) Show those employee name and date of birth who have served more than 17 years as on date.
128 Pune 13-mar-2008 7500 13-mar-2028 Sales (m) Show names of those who earn more than all of the employees of Sales dept.
(n) Increase salary of the employees by 5 % of their present salary with hobby as Music or they have
129 Bangalore 21-july-2003 7000 21-july-2023 Finance completed atleast 3 years of service.
(a) Show empno, name and salary of those who have Sports as hobby. (o) Write the output of :
(b) Show name of the eldest employee. 1. Select distinct hobby from personal ;
(c) Show number of employee area wise. 2. Select avg(salary) from personal, job where Personal.Empno = Job.Sno and Area in ('Agra','Delhi') ;
(d) Show youngest employees from each Native place. 3. Select count(distinct Native_place) from personal.
(e) Show Sno, Name, Hobby and Salary in descending order of Salary. 4. Select name, max(salary) from Personal, Job where Personal.Empno = Job.Sno;
(f) Show the hobbies of those whose name pronounces as 'Abhay'. (p) Add a new tuple in the table Personal essentially with hobby as Music.
(q) Insert a new column email in Job table | 129 | Ramesh | 7000 |
(r) Create a table with values of columns empno, name, and hobby. +-------+--------+--------+
(s) Create a view of Personal and Job details of those who have served less than 15 years. (b)
(t) Erase the records of employee from Job table whose hobby is not Sports. SELECT name
(u) Remove the table Personal. FROM personal
Answer WHERE dobirth = (
(a) SELECT MIN(dobirth)
SELECT P.EMPNO, P.NAME, J.Salary FROM personal
FROM PERSONAL P, JOB J );
WHERE P.EMPNO = J.Sno AND P.Hobby = 'Sports'; Output
Output +------+
+-------+--------+--------+ | name |
| EMPNO | NAME | Salary | +------+
+-------+--------+--------+ | Amit |
| 125 | Vinod | 8500 | +------+
(c) SELECT Name, `Native-place`, dobirth
SELECT Area, COUNT(Sno) AS Employee_Count FROM personal
FROM Job WHERE dobirth = (SELECT MAX(dobirth)
GROUP BY Area; FROM personal p2
Output WHERE personal.`Native-place` = p2.`Native-place` ) ;
+-----------+----------------+ Output
| Area | Employee_Count | +--------+--------------+------------+
+-----------+----------------+ | Name | Native-place | dobirth |
| Agra | 2| +--------+--------------+------------+
| Delhi | 1| | Abhai | Allahabad | 1975-08-11 |
| Mathura | 1| | Vinod | Delhi | 1977-04-04 |
| Pune | 1| | Manoj | Mumbai | 1976-12-12 |
| Bangalore | 1| | Ramesh | Pune | 1981-10-28 |
+-----------+----------------+ +--------+--------------+------------+
(d) (e)
+-------------------+ | Sports |
| Additional Burden | +--------+
+-------------------+ (k)
| 1550.0 | SELECT COUNT(*)
+-------------------+ FROM Job
(j) WHERE DATEDIFF(CURDATE(), App_date) >= 20 * 365;
SELECT Hobby Output
FROM PERSONAL +----------+
GROUP BY Hobby | COUNT(*) |
HAVING COUNT(*) >= 2; +----------+
Output | 0|
+--------+ +----------+
| Hobby | (l)
+--------+ SELECT P.Name, P.Dobirth
| Music | FROM Personal P, Job J
WHERE P.Empno = J.Sno AND J.Retd_date > CURDATE() AND DATEDIFF(CURDATE(), J.App_date) > 17 * 365; Explanation
Output There will be no output because there are no employees whose salary is greater than the highest salary in
the Sales department.
+-------+------------+
(n)
| Name | Dobirth |
+-------+------------+ UPDATE Job J, Personal P
SET J.Salary = (J.Salary * 0.05 ) + J.Salary
| Amit | 1965-01-23 |
WHERE J.Sno = P.Empno
| Manoj | 1976-12-12 |
+-------+------------+ AND (P.Hobby = 'Music'
With reference to the table below, answer the questions that follow : 441 Peter Thompson 11 Red Road Paris
Table : Employees
Table : EmpSalary
Empid Firstname Lastname Address City
Empid Salary Benefits Designation
010 Ravi Kumar Raj nagar GZB
010 75000 15000 Manager
105 Harry Waltor Gandhi nagar GZB
105 65000 15000 Manager
152 Sam Tones 33 Elm St. Paris
152 80000 25000 Director
215 Sarah Ackerman 440 U.S. 110 Upton
215 75000 12500 Manager
244 Manila Sengupta 24 Friends street New Delhi
244 50000 12000 Clerk
300 Robert Samuel 9 Fifth Cross Washington
With reference to the table below, answer the questions that follow : 441 Peter Thompson 11 Red Road Paris
Table : Employees
Table : EmpSalary
Empid Firstname Lastname Address City
Empid Salary Benefits Designation
010 Ravi Kumar Raj nagar GZB
010 75000 15000 Manager
105 Harry Waltor Gandhi nagar GZB
105 65000 15000 Manager
152 Sam Tones 33 Elm St. Paris
Empid Salary Benefits Designation (iv) Select sum(Benefits) from EmpSalary where Designation = 'Clerk' ;
Answer
152 80000 25000 Director
(i)
215 75000 12500 Manager Output
| 4| | 32000 |
+-----------------------------+ +---------------+
(iii) Question 15
Output Show the average salary for all departments with more than 3 people for a job.
+-------------+-------------+ Answer
| DESIGNATION | SUM(SALARY) | SELECT job, AVG(Sal) AS AvgSalary
+-------------+-------------+ FROM Empl
| MANAGER | 215000 | GROUP BY job HAVING COUNT(*) > 3;
| CLERK | 135000 | Output
+-------------+-------------+ +----------+-----------+
(iv) | job | AvgSalary |
Output +----------+-----------+
+---------------+ | CLERK | 1037.5 |
| SUM(BENEFITS) | | SALESMAN | 1400 |
+---------------+ +----------+-----------+
Question 16 Answer
Display only the jobs with maximum salary greater than or equal to 3000. SELECT COUNT(*) AS NumManagers
Answer FROM EmpSalary
SELECT Job WHERE Designation = 'Manager';
FROM Empl Output
GROUP BY Job HAVING MAX(Sal) >= 3000; +-------------+
Output | NumManagers |
+-----------+ +-------------+
| Job | | 3|
+-----------+ +-------------+
| PRESIDENT | Question 18
| ANALYST | List the count of employees grouped by deptno. (table EMPL)
+-----------+ Answer
Question 17 SELECT deptno, COUNT(*) AS employee_count
Find out number of employees having "Manager" as Job. FROM EMPL
| 30 | 2850 | zip
| 10 | 5000 |
Orders
+--------+------------+
order id (PK)
Question 21
Below are the customer and order tables : order_date
Customers amount
customer id (PK)
customer_id (FK)
first_name List the total of customers' orders grouped by customer (id).
last_name Answer
SELECT c.customer_id, COUNT(o.order_id) AS total_orders
email
FROM Customers c, orders o
last_name List the sum of the totals of orders grouped by customer and state.
Answer
email
SELECT c.customer_id, c.state, SUM(o.amount) AS total_order_amount
address FROM Customers c, orders o
last_name Answer
SELECT CONCAT(c.first_name, ' ', c.last_name) AS customer_name,
email
SUM(o.amount) AS total_order_amount
address FROM Customers c, Orders o
order id (PK)
SALARYGRADE (Lowsal, Highsal, Grade)
order_date