0% found this document useful (0 votes)
24 views21 pages

CSC CHP 13

The document discusses SQL concepts including the GROUP BY clause, differences between HAVING and WHERE clauses, and aggregate functions. It explains how to use SQL joins to combine data from multiple tables and provides examples of SQL statements for retrieving specific information from tables. Additionally, it outlines various SQL queries to calculate sales amounts and average exam scores based on specific conditions.

Uploaded by

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

CSC CHP 13

The document discusses SQL concepts including the GROUP BY clause, differences between HAVING and WHERE clauses, and aggregate functions. It explains how to use SQL joins to combine data from multiple tables and provides examples of SQL statements for retrieving specific information from tables. Additionally, it outlines various SQL queries to calculate sales amounts and average exam scores based on specific conditions.

Uploaded by

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

Question 1 The GROUP BY clause in SQL is used to combine all records that have identical values in a particular field or

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

Column Name SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES


GROUP BY STORE_ID HAVING SUM(SALES_AMOUNT) > 5000;
STORE_ID
Explanation
SALES_DATE 1. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING
SUM(SALES_AMOUNT) > 5000; — This statement selects the store ID and calculates the total sales
SALES_AMOUNT amount for each store using the SUM() aggregate function. The GROUP BY STORE_ID clause ensures
Which SQL statement lets you list all stores whose total sales amount is over 5000 ? that the results are grouped by store ID. The HAVING SUM(SALES_AMOUNT) > 5000 condition then
filters the grouped data, selecting only those stores whose total sales amount is over 5000.
1. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING
SUM(SALES_AMOUNT) > 5000; 2. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING SALES_AMOUNT
> 5000; — This option is incorrect because the HAVING clause cannot directly reference
SALES_AMOUNT without an aggregate function like SUM() since SUM(SALES_AMOUNT) is used in the SALES_AMOUNT
SELECT statement.
Which SQL statement lets you find the total number of stores in the SALES table?
3. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE SUM(SALES_AMOUNT) > 5000 GROUP
BY STORE_ID; — This option is incorrect because aggregate functions like SUM(SALES_AMOUNT) 1. SELECT COUNT(STORE_ID) FROM SALES;
cannot be used directly in the WHERE clause. The WHERE clause is used for filtering individual rows 2. SELECT COUNT(DISTINCT STORE_ID) FROM SALES;
before grouping.
3. SELECT DISTINCT STORE_ID FROM SALES;
4. SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE SALES_AMOUNT > 5000 GROUP BY
STORE_ID; — This option is incorrect because it tries to filter individual sales amounts 4. SELECT COUNT(STORE_ID) FROM SALES GROUP BY STORE_ID;
(SALES_AMOUNT) directly without using the SUM() aggregate function to calculate the total sales Answer
amount for each store. The GROUP BY STORE_ID clause is used for grouping after the filtering, which
SELECT COUNT(DISTINCT STORE_ID) FROM SALES;
is not the correct approach for filtering based on the total sales amount.
Explanation
Question 4
1. SELECT COUNT(STORE_ID) FROM SALES; — This query uses the COUNT() aggregate function with the
Table SALES
STORE_ID column in the SELECT statement. It counts the number of non-null values in the STORE_ID
Column Name column, and this count includes duplicates.
2. SELECT COUNT(DISTINCT STORE_ID) FROM SALES; — This option uses COUNT(DISTINCT STORE_ID) to
STORE_ID count the number of unique store IDs in the SALES table. The DISTINCT keyword ensures that only
distinct (unique) values are counted, avoiding overcounting due to duplicates.
SALES_DATE

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.

+-----------------+ 4. SELECT COUNT(EXAM_SCORE) FROM EXAM_RESULTS WHERE EXAM_ID = 1; — This statement


calculates the count of exam scores for EXAM_ID = 1, but it doesn't calculate the average score.
| AVG(EXAM_SCORE) |
Question 7
+-----------------+
Table EXAM_RESULTS
| 83.2500 |
+-----------------+ STU ID FNAME LNAME EXAM ID EXAM_SCORE

Explanation 10 LAURA LYNCH 1 90


1. SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS; — This statement calculates the average exam
score across all exam IDs in the EXAM_RESULTS table. 10 LAURA LYNCH 2 85

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

STU ID FNAME LNAME EXAM ID EXAM_SCORE | 95 |


+-----------------+
13 WILLIAM BISHOP 2 100
Explanation
14 CHARLES PRADA 2 85 The above SQL query calculates the maximum exam score for EXAM_ID 1 from the EXAM_RESULTS table,
by grouping results based on EXAM_ID and filtering using HAVING.
What is the result of the following SQL statement ?
Question 10
SELECT MAX(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID HAVING EXAM_ID = 1;
Given the following table :
1. 90
Table : CLUB
2. 85
3. 100 COACH-
COACHNAME AGE SPORTS DATOFAPP PAY SEX
ID
4. 95
Answer 1 KUKREJA 35 KARATE 27/03/1996 1000 M
Output 2 RAVINA 34 KARATE 20/01/1998 1200 F
+-----------------+
3 KARAN 34 SQUASH 19/02/1998 2000 M
| MAX(EXAM_SCORE) |
+-----------------+ 4 TARUN 33 BASKETBALL 01/01/1998 1500 M
COACH- Answer
COACHNAME AGE SPORTS DATOFAPP PAY SEX
ID 1.

5 ZUBIN 36 SWIMMING 12/01/1998 750 M Output


+------------------------+
6 KETAKI 36 SWIMMING 24/02/1998 800 F
| COUNT(DISTINCT SPORTS) |
7 ANKITA 39 SQUASH 20/02/1998 2200 F +------------------------+

8 ZAREEN 37 KARATE 22/02/1998 1100 F | 4|


+------------------------+
9 KUSH 41 SWIMMING 13/01/1998 900 M
Explanation
10 SHAILYA 37 BASKETBALL 19/02/1998 1700 M The SQL query SELECT COUNT(DISTINCT SPORTS) FROM Club ; calculates the count of unique values in the
'SPORTS' column of the 'Club' table. This query helps us to get information about the number of sports
Give the output of following SQL statements :
offered by the club.
1. SELECT COUNT(DISTINCT SPORTS) FROM Club ;
2.
2. SELECT MIN(Age) FROM CLUB WHERE Sex = 'F' ;
Output
3. SELECT AVG(Pay) FROM CLUB WHERE Sports = 'KARATE' ;
+----------+
4. SELECT SUM(Pay) FROM CLUB WHERE Datofapp > '1998-01-31' ;
| MIN(Age) |

+----------+ 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.

SELECT EMPLOYEE.EMPLOYEEID, EMPLOYEE.NAME, EMPLOYEE.JOBID, JOB.JOBTITLE FROM EMPLOYEE, JOB


FROM EMPLOYEE, JOB WHERE EMPLOYEE.JOBID = JOB.JOBID
WHERE EMPLOYEE.JOBID = JOB.JOBID; AND EMPLOYEE.SALES > 1300000;
Output Output
+------------+-------------------+-------+--------------------------+ +---------------+---------+--------------------------+
| EMPLOYEEID | NAME | JOBID | JOBTITLE | | NAME | SALES | JOBTITLE |
+------------+-------------------+-------+--------------------------+ +---------------+---------+--------------------------+
| E1 | SUMIT SINHA | 102 | VICE PRESIDENT | | AJAY RAJPAL | 1400000 | ADMINISTARTION ASSISTANT |
| E2 | VIJAY SINGH TOMAR | 101 | PRESIDENT | | SHAILJA SINGH | 1450000 | ADMINISTARTION ASSISTANT |
| E3 | AJAY RAJPAL | 103 | ADMINISTARTION ASSISTANT | +---------------+---------+--------------------------+
| E4 | MOHIT RAMNANI | 102 | VICE PRESIDENT | 3.
| E5 | SHAILJA SINGH | 103 | ADMINISTARTION ASSISTANT | SELECT EMPLOYEE.NAME, JOB.JOBTITLE
+------------+-------------------+-------+--------------------------+ FROM EMPLOYEE, JOB
2. WHERE EMPLOYEE.JOBID = JOB.JOBID
SELECT EMPLOYEE.NAME, EMPLOYEE.SALES, JOB.JOBTITLE AND EMPLOYEE.NAME LIKE '%SINGH%';
Output | EMPLOYEEID | NAME | SALES | JOBID |
+-------------------+--------------------------+ +------------+-------------------+---------+-------+
| NAME | JOBTITLE | | E1 | SUMIT AINHA | 1100000 | 102 |
+-------------------+--------------------------+ | E2 | VIJAY SINGH TOMAR | 1300000 | 101 |
| VIJAY SINGH TOMAR | PRESIDENT | | E3 | AJAY RAJPAL | 1400000 | 103 |
| SHAILJA SINGH | ADMINISTARTION ASSISTANT | | E4 | MOHIT RAMNANI | 1250000 | 104 |
+-------------------+--------------------------+ | E5 | SHAILJA SINGH | 1450000 | 103 |
4. In the given tables, EMPLOYEE and JOB, the JOBID column in the EMPLOYEE table is a foreign key +------------+-------------------+---------+-------+
referencing the JOBID column in the JOB table. Question 12
5.
Consider the following tables Employee and Salary. Write SQL commands for the statements (i) to (iv) and
UPDATE EMPLOYEE give outputs for SQL queries (v) to (vii)
SET JOBID = 104 Table : Employee
WHERE EMPLOYEEID = 'E4'; Eid Name Depid Qualification Sec
Output
1 Deepali Gupta 101 MCA F
SELECT * FROM EMPLOYEE ;
+------------+-------------------+---------+-------+

Eid Name Depid Qualification Sec Eid Basic D.A HRA Bonus

2 Rajat Tyagi 101 BCA M 4 1500 390 490 30

3 Hari Mohan 102 B.A. M 5 8000 900 900 80

4 Harry 102 M.A. M 6 10000 300 490 89

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)

SELECT SNO, NAME, HOBBY, SALARY (f)


FROM PERSONAL, JOB SELECT HOBBY
WHERE PERSONAL.EMPNO = JOB.SNO FROM PERSONAL
ORDER BY SALARY DESC; WHERE Name = 'abhay' or Name = 'abhai' ;
Output Output
+-----+--------+-----------+--------+ +-----------+
| SNO | NAME | HOBBY | SALARY | | HOBBY |
+-----+--------+-----------+--------+ +-----------+
| 125 | Vinod | Sports | 8500 | | Music |
| 128 | Abhay | Gardening | 7500 | | Gardening |
| 129 | Ramesh | Sports | 7000 | +-----------+
| 127 | Manoj | Writing | 6000 | (g)
| 124 | Abhai | Music | 5500 | SELECT App_date, nativeplace
| 123 | Amit | Music | 5000 | FROM personal, job
+-----+--------+-----------+--------+ WHERE personal.empno = job.sno
AND (Name LIKE 'A%' OR Name LIKE '%d') ; +----------------+
Output | Salary Expense |
+------------+--------------+ +----------------+
| App_date | native-place | | 5000 |
+------------+--------------+ | 5500 |
| 2006-01-25 | Delhi | | 8500 |
| 2007-08-19 | Allahabad | | 6000 |
| 2004-04-14 | Delhi | | 7500 |
| 2008-03-13 | Mumbai | | 7000 |
+------------+--------------+ +----------------+
(h) (i)
SELECT Salary AS "Salary Expense" SELECT SUM(Salary * 0.1) AS "Additional Burden"
FROM Job FROM PERSONAL, JOB
WHERE `Retd_date` > '2006-01-20'; WHERE PERSONAL.EMPNO = JOB.SNO AND HOBBY = 'SPORTS' ;
Output Output

+-------------------+ | 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'

(m) OR DATEDIFF(CURDATE(), J.App_date) >= 3 * 365);


Output
SELECT Name
+-----+-----------+------------+--------+------------+-----------+
FROM Personal, job
where personal.Empno = job.Sno | sno | area | app_date | salary | retd_date | dept |
+-----+-----------+------------+--------+------------+-----------+
and job.Salary > ( select max(salary)
| 123 | Agra | 2006-01-25 | 5250 | 2026-01-25 | Marketing |
from job
where dept = 'sales'); | 124 | Agra | 2007-08-19 | 5775 | 2027-08-19 | Marketing |
| 125 | Delhi | 2004-04-14 | 8925 | 2018-04-14 | Sales |

| 127 | Mathura | 2006-12-22 | 6300 | 2026-12-22 | Finance | +-----------+


| 128 | Pune | 2008-03-13 | 7875 | 2028-03-13 | Sales | 2.
| 129 | Bangalore | 2003-07-21 | 7350 | 2023-07-21 | Finance | Select avg(salary) from personal, job where Personal.Empno = Job.Sno and Area in ('Agra','Delhi') ;
+-----+-----------+------------+--------+------------+-----------+ Output
(o) +-------------+
1. | AVG(SALARY) |
Select distinct hobby from personal ; +-------------+
Output | 6650.0000 |
+-----------+ +-------------+
| hobby | 3.
+-----------+ Select count(distinct Native_place) from personal;
| Music | Output
| Sports | +--------------------------------+
| Writing | | COUNT(DISTINCT `NATIVE-PLACE`) |
| Gardening | +--------------------------------+
| 4| record of the group for the 'name' field. Therefore, 'Amit' is the first record in the 'Personal' table, the
+--------------------------------+ query returns 'Amit' as the value for the 'name' field.
(p)
4.
INSERT INTO Personal (Empno, Name, Dobirth, `Native-place`, Hobby)
Select name, max(salary)
from Personal, Job where Personal.Empno = Job.Sno ; VALUES (130, 'Amruta', '1990-05-15', 'Chennai', 'Music');
Output
Output
select * from personal;
+------+-------------+
| name | max(salary) | +-------+--------+------------+--------------+-----------+

+------+-------------+ | empno | name | dobirth | native-place | hobby |


+-------+--------+------------+--------------+-----------+
| Amit | 8500 |
| 123 | Amit | 1965-01-23 | Delhi | Music |
+------+-------------+
Explanation | 124 | Abhai | 1975-08-11 | Allahabad | Music |
| 125 | Vinod | 1977-04-04 | Delhi | Sports |
The given query retrieves the maximum salary from the 'Job' table and pairs it with the corresponding
employee name from the 'Personal' table based on the matching Empno and Sno values. However, | 127 | Manoj | 1976-12-12 | Mumbai | Writing |
including a non-group field like 'name' in the select-list means it will return the value from the first
| 128 | Abhay | 1974-03-10 | Mumbai | Gardening |
| 129 | Ramesh | 1981-10-28 | Pune | Sports |

| 130 | Amruta | 1990-05-15 | Chennai | Music | | 124 | Abhai | Music |


+-------+--------+------------+--------------+-----------+ | 125 | Vinod | Sports |
(q) | 127 | Manoj | Writing |
ALTER TABLE Job | 128 | Abhay | Gardening |
ADD COLUMN Email VARCHAR(55); | 129 | Ramesh | Sports |
(r) | 130 | Amruta | Music |
insert into empdetails(empno, name, hobby) +-------+--------+-----------+
select empno, name, hobby (s)
from personal ; CREATE VIEW LessThan15YearsView AS
Output SELECT * FROM Personal p, Job j
select * from empdetails ; WHERE p.Empno = j.Sno AND
+-------+--------+-----------+ DATEDIFF(CURDATE(), J.App_date) < 15 * 365;
| Empno | Name | Hobby | (t)
+-------+--------+-----------+ DELETE j
| 123 | Amit | Music | FROM Job j, Personal p
WHERE j.Sno = p.Empno AND p.Hobby <> 'Sports'; Empid Firstname Lastname Address City
(u)
335 Ritu Tondon Shastri Nagar GZB
DROP TAbLE Personal;
Question 14a 400 Rachel Lee 121 Harrison St. New York

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

Empid Salary Benefits Designation FROM Employees


WHERE City = 'Paris';
300 45000 10000 Clerk
Output
335 40000 10000 Clerk +-----------+----------+-------------+-------+

400 32000 7500 Salesman | Firstname | Lastname | Address | City |


+-----------+----------+-------------+-------+
441 28000 7500 Salesman
| SAM | TONES | 33 ELM ST. | PARIS |
Write the SQL commands for the following using above tables :
| PETER | THOMPSON | 11 RED ROAD | PARIS |
(i) To show firstname, lastname, address and city of all employees living in Pairs.
+-----------+----------+-------------+-------+
(ii) To display the content of Employees table in descending order of Firstname.
(ii)
(iii) To display the firstname, lastname and total salary of all managers from the tables Employes and
SELECT *
EmpSalary, where total salary is calculated as Salary + Benefits.
FROM Employees
(iv) To display the maximum salary among managers and clerks from the table EmpSalary.
ORDER BY Firstname DESC;
Answer
Output
(i)
+-------+-----------+----------+-------------------+------------+
SELECT Firstname, Lastname, Address, City
| empid | FIRSTNAME | LASTNAME | ADDRESS | CITY | FROM Employees e, EmpSalary s
+-------+-----------+----------+-------------------+------------+ WHERE e.Empid = s.Empid AND s.Designation = 'Manager';
| 215 | SARAH | ACKERMAN | 440 U.S. 110 | UPTON | Output
| 152 | SAM | TONES | 33 ELM ST. | PARIS | +-----------+----------+-------------+
| 300 | ROBERT | SAMUEL | 9 FIFTH CROSS | WASHINGTON | | Firstname | Lastname | TotalSalary |
| 335 | RITU | TONDON | SHASTRI NAGAR | GZB | +-----------+----------+-------------+
| 10 | RAVI | KUMAR | RAJ NAGAR | GZB | | RAVI | KUMAR | 90000 |
| 400 | RACHEL | LEE | 121 HARRISON ST. | NEW YORK | | HARRY | WALTOR | 80000 |
| 441 | PETER | THOMPSON | 11 RED ROAD | PARIS | | SARAH | ACKERMAN | 87500 |
| 244 | MANILA | SENGUPTA | 24 FRIENDS STREET | NEW DELHI | +-----------+----------+-------------+
| 105 | HARRY | WALTOR | GANDHI NAGAR | GZB | (iv)
+-------+-----------+----------+-------------------+------------+ SELECT Designation, MAX(Salary)
(iii) FROM EmpSalary
SELECT e.Firstname, e.Lastname, WHERE Designation IN ('Manager', 'Clerk')
(s.Salary + s.Benefits) AS TotalSalary group by designation;

Output Empid Firstname Lastname Address City


+-------------+-------------+
215 Sarah Ackerman 440 U.S. 110 Upton
| Designation | MAX(Salary) |
+-------------+-------------+ 244 Manila Sengupta 24 Friends street New Delhi

| MANAGER | 75000 | 300 Robert Samuel 9 Fifth Cross Washington


| CLERK | 50000 |
335 Ritu Tondon Shastri Nagar GZB
+-------------+-------------+
Question 14b 400 Rachel Lee 121 Harrison St. New York

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

244 50000 12000 Clerk +-----------+--------+


| FIRSTNAME | SALARY |
300 45000 10000 Clerk
+-----------+--------+
335 40000 10000 Clerk | RACHEL | 32000 |

400 32000 7500 Salesman | PETER | 28000 |


+-----------+--------+
441 28000 7500 Salesman
(ii)
Give the Output of following SQL commands :
Output
(i) Select firstname, Salary from Employees, Empsalary where Designation = 'Salesman' and
+-----------------------------+
Employees.Empid = Empsalary.Empid ;
| COUNT(DISTINCT DESIGNATION) |
(ii) Select count(distinct designation) from EmpSalary ;
+-----------------------------+
(iii) Select designation, sum(salary) from EmpSalary group by designation having count(*) > 2 ;

| 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

GROUP BY deptno; Output


Output +--------+--------------+
+--------+----------------+ | deptno | total_salary |
| deptno | employee_count | +--------+--------------+
+--------+----------------+ | 20 | 10885 |
| 20 | 5| | 30 | 9400 |
| 30 | 6| | 10 | 8750 |
| 10 | 3| +--------+--------------+
+--------+----------------+ Question 20
Question 19 List the maximum salary of employee grouped by their department number.
List the sum of employees' salaries grouped by department. (table EMPL) Answer
Answer SELECT deptno, MAX(sal) AS max_salary
SELECT deptno, SUM(sal) AS total_salary FROM EMPL
FROM EMPL GROUP BY deptno;
GROUP BY deptno; Output
+--------+------------+ address
| deptno | max_salary |
city
+--------+------------+
| 20 | 3000 | state

| 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

WHERE c.customer_id = o.customer_id zip


GROUP BY c.customer_id;
Orders
Question 22
order id (PK)
Below are the customer and order tables :
Customers order_date

customer id (PK) amount

first_name customer_id (FK)

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

city WHERE c.customer_id = o.customer_id


GROUP BY c.customer_id, c.state;
state
Question 23
Below are the customer and order tables :
Customers amount
customer id (PK)
customer_id (FK)
first_name List the customers (name) and the total amount of all their orders.

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

city WHERE c.customer_id = o.customer_id


GROUP BY c.customer_id;
state
Question 24
zip Schemas of tables EMPL, Dept, SalaryGrade are being shown below :
Orders EMPL (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

order id (PK)
SALARYGRADE (Lowsal, Highsal, Grade)
order_date

DEPT (Deptno, DeptName, Location) Answer


List the department names and the number of their employees. SELECT e.ENAME AS Employee_Name, d.DeptName AS Department_Name
Answer FROM EMPL e, DEPT d
SELECT d.DeptName AS Department_Name, COUNT(e.EMPNO) AS Number_of_Employees WHERE e.DEPTNO = d.Deptno;
FROM DEPT d, EMPL e
WHERE d.Deptno = e.DEPTNO
GROUP BY d.DeptName;
Question 25
Schemas of tables EMPL, Dept, SalaryGrade are being shown below :
EMPL (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

SALARYGRADE (Lowsal, Highsal, Grade)

DEPT (Deptno, DeptName, Location)


List the employee names and the name of their departments.

You might also like