Chap 13 Short Notes
Chap 13 Short Notes
Mrs AnithaMCA.,MPhil.,,Pgdca,
Chap 13: Grouping Records,
Joins in SQL
Class 12 - Computer Science with Python
Sumita Arora
Question 1
A ............... is a query that retrieves rows from more than one table or view:
1. Start
2. End
3. Join
4. All of these
Answer
Join
Reason — A join is a query that combines rows from two or more tables or views. In a join-
query, more than one table are listed in FROM clause.
Question 2
Answer
Acts like a WHERE clause but is used for groups rather than rows.
Reason — The HAVING clause places conditions on groups in contrast to WHERE clause that
places conditions on individual rows.
Question 3
Aggregate functions can be used in the select list or the ............... clause of a select statement.
They cannot be used in a ............... clause.
1. Where, having
2. Having, where
3. Group by, having
4. Group by, where
Answer
Having, where
Reason — Aggregate functions can be used in the select list or the HAVING clause of a select
statement. But they cannot be used in a WHERE clause. The reason for this is that the
WHERE clause filters rows before any grouping or aggregation occurs, while HAVING clause
applies conditions to groups after the data has been grouped using the GROUP BY clause.
Question 4
SQL applies conditions on the groups through ............... clause after groups have been
formed.
1. Group by
2. With
3. Where
4. Having
Answer
Having
Reason — The HAVING clause applies conditions to groups after the data has been grouped
using the GROUP BY clause in SQL queries.
Question 5
1. GROUP BY
2. SELECT
3. WHERE
4. Both (1) and (3)
Answer
Reason — Aggregate functions are used with the GROUP BY clause to perform calculations
on groups of rows. However, they are also used in the SELECT clause to display the results of
aggregate calculations.
Question 6
Answer
To filter out the summary groups
Reason — The HAVING clause is used to filter out summary groups in a SELECT query that
involves aggregate functions and the GROUP BY clause.
Question 7
1. True
2. False
3. Only in views
4. With order by
Answer
False
Reason — The HAVING clause places conditions on groups in contrast to WHERE clause that
places conditions on individual rows. While WHERE conditions cannot include aggregate
functions, HAVING conditions can include aggregate functions. Hence, WHERE and HAVING
clauses cannot be used interchangeably in SELECT queries.
Question 8
The operation whose result contains all pairs of tuples from the two relations, regardless of
whether their attribute values match.
1. Join
2. Cartesian product
3. Intersection
4. Set difference
Answer
Cartesian product
Question 9
Which SQL function is used to count the number of rows in a SQL query ?
1. COUNT()
2. NUMBER()
3. SUM()
4. COUNT(*)
Answer
COUNT(*)
Reason — The SQL function COUNT(*) is used to count the number of rows in an SQL query,
including duplicates and rows with NULL values.
Question 10
1. Avg
2. Sum
3. With
4. Min
Answer
With
Reason — Aggregate functions in SQL include AVG, COUNT, MAX, MIN, and SUM.
Question 11
All aggregate functions except ............... ignore null values in their input collection.
1. Count(attribute)
2. Count(*)
3. Avg
4. Sum
Answer
Count(*)
Reason — All aggregate functions, except for COUNT(*), ignore null values in their input
collection. COUNT(*) returns all rows, including duplicates and nulls.
Question 12
1. LEFT
2. AVG
3. JOIN
4. LEN
Answer
AVG
Reason — Aggregate functions in SQL include AVG, COUNT, MAX, MIN, and SUM.
Question 13
1. MAX
2. COUNT
3. SUM
4. All of the above
Answer
All of the above
Reason — All aggregate functions, except for COUNT(*), ignore null values in their input
collection.
Question 14
The functions which work with individual rows' data are called ............... function.
1. Single row
2. Multiple rows
3. Aggregate
4. None of these
Answer
Single row
Reason — Single-row functions in SQL work with one row at a time and return a result for
every row of a queried table.
Question 15
1. Single row
2. Multiple rows
3. Aggregate
4. None of these
Answer
Aggregate
Question 16
1. Group by
2. With
3. Where
4. Having
Answer
Having
Reason — When using the SUM() function in a condition, it is used with the HAVING clause.
The HAVING clause is used to apply conditions to groups of rows after the GROUP BY
operation has been performed, making it suitable for conditions involving aggregate functions
like SUM().
Question 17
Answer
WHERE
Reason — Aggregate functions cannot be used with the WHERE clause in SQL because the
WHERE clause filters rows before any grouping or aggregation occurs.
Question 18
Answer
To filter out the row and column values before creating groups
Reason — The WHERE clause is used to filter rows and columns values before any grouping
occurs in a query with a GROUP BY clause.
Question 19
1. Equi-join
2. Natural join
3. Outer join
4. Cartesian product
Answer
Equi-join
Reason — An equi-join is a type of join where columns from two or more tables are compared
for equality using the "=" operator. In the given SQL query, the WHERE clause specifies the
condition CUSTOMER.CUST_ID = ORDER.CUST_ID , which compares the CUST_ID column
from the CUSTOMER table with the CUST_ID column from the ORDER table. Therefore, it is
an equi-join.
Question 20
Answer
Cartesian product
Reason — This query uses a comma-separated list of table names in the FROM clause without
specifying any join condition. In SQL, when we list multiple tables in the FROM clause separated
by commas without specifying a join condition, it results in a Cartesian product.
Question 21
1. Equijoins
2. Cartesian product
3. Both (1) and (2)
4. None of the mentioned
Answer
Cartesian product
Reason — When a join query has no join condition specified, it results in a Cartesian product.
This means that every row from the first table is combined with every row from the second
table.
Question 22
1. Equijoins
2. Cartesian product
3. Both (1) and (2)
4. None of the mentioned
Answer
Equijoins
Reason — An equi-join is a type of join where columns from two or more tables are compared
for equality using the "=" operator.
Question 1
Question 2
A JOIN is a means for combining fields from two tables by using values common to each.
Question 4
Question 5
Question 6
Question 7
The SQL built-in function AVG computes the average of values in numeric columns.
Question 8
The SQL built-in function MAX obtains the largest value in a numeric column.
Question 9
The SQL built-in function MIN obtains the smallest value in a numeric column.
Question 10
The SQL built-in function COUNT computes the number of rows in a table.
Question 11
The SELECT clause GROUP BY is used to collect those rows that have the same value in a
specified column.
Question 12
Question 13
To create a summary of records based on the common value in a field in different rows of the
table GROUP BY clause is used.
Question 14
To get data from two or more tables having some common fields, join query is created.
Question 15
In equi-join, the join condition joins the two table using = operator.
True/False Questions
Question 1
The HAVING clause acts like a WHERE clause, but it identifies groups that meet a criterion,
rather than rows.
Answer
True
Reason — The HAVING clause in SQL is used to filter groups based on specified conditions,
while the WHERE clause filters individual rows. This means that the HAVING clause works
with grouped data, applying conditions to groups that meet certain criteria, whereas the
WHERE clause applies conditions to individual rows before any grouping occurs.
Question 2
Data manipulation language (DML) commands are used to define a database, including
creating, altering, and dropping tables and establishing constraints.
Answer
False
Reason — Data Definition Language (DDL) commands are used to define a database,
including creating, altering, and dropping tables and establishing constraints.
Question 3
The SQL keyword GROUP BY instructs the DBMS to group together those rows that have the
same value in a column.
Answer
True
Reason — The SQL keyword GROUP BY clause instructs the DBMS to combine all those
rows that have identical values in a particular column or a group of columns.
Question 4
Equi join can use any operator for joining two tables.
Answer
False
Reason — An equi-join is a type of join where columns from two or more tables are compared
for equality using the "=" operator.
Question 5
Answer
True
Reason — When a join query has no join condition specified, it results in a Cartesian product.
This means that every row from the first table is combined with every row from the second
table.
Question 6
COUNT(field_name) tallies only those rows that contain a value; it ignores all null values.
Answer
True
Reason — When we use COUNT(field_name), it counts only the rows where the specified field
(field_name) is not null. It does ignore null values for that specific field during counting.
Question 7
SUM, AVG, MIN, and MAX can only be used with numeric columns.
Answer
True
Reason — The aggregate functions SUM, AVG, MIN, and MAX are designed to work with
numeric columns in SQL. They expect numeric values as arguments and return numeric
results.
Question 8
The SQL keyword GROUP BY instructs the DBMS to group together those rows that have the
same value in a column.
Answer
True
Reason — The SQL keyword GROUP BY instructs the DBMS to combine all those rows that
have identical values in a particular column or a group of columns.
Question 9
Answer
False
Reason — The HAVING clause places conditions on groups in contrast to WHERE clause that
places conditions on individual rows. While WHERE conditions cannot include aggregate
functions, HAVING conditions can include aggregate functions. Hence, WHERE and HAVING
clauses cannot be used interchangeably in queries.
Question 10
The HAVING clauses can take any valid SQL function in its condition.
Answer
False
Reason — The HAVING clause can contain either a simple boolean expression (i.e., an
expression or condition that results into true or false) or use aggregate function in the having
condition.
Question 11
HAVING clause can only be used if the SELECT query has GROUP BY clause.
Answer
True
Reason — The HAVING clause in SQL works with grouped data, applying conditions to groups
that meet certain criteria. Therefore, HAVING clause can only be used if the SELECT query
has GROUP BY clause.
Question 12
With GROUP BY, the select-list of the SELECT statement can only take the group-field and/or
aggregate function.
Answer
True
Reason — When using the GROUP BY clause in SQL, the select-list of the SELECT
statement can only include the grouping columns (group-fields) and/or aggregate functions.
MySQL would not produce any error even if we include a non-group field in the select-list.
However, it will return the value from first record of the group for that non-group field.
Question 13
Answer
True
Reason — When a join query has no join condition specified, it results in a Cartesian product.
This means that every row from the first table is combined with every row from the second
table.
Question 14
You can specify any type of condition, using any comparison operator in an equi-join.
Answer
False
Reason — An equi-join is a type of join where columns from two or more tables are compared
for equality using the "=" operator.
Question 15
Answer
False
Reason — A join in SQL is a query that combines rows from two or more tables.
Question 1
Assertion. SQL SELECT's GROUP BY clause is used to divide the result in groups.
Reason. The GROUP BY clause combines all those records that have identical values in a
particular field or in group by fields.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
The GROUP BY clause combines all those records that have identical values in a particular
field or a group of fields. This clause is utilised in SELECT statements to divide the result set
into groups based on the values in the specified columns. Grouping can be done by a column
name, or with aggregate functions in which case the aggregate produces a value for each
group.
Question 2
Answer
(c)
Explanation
A GROUP BY query can include functions. This is true because SQL allows the use of
aggregate functions like COUNT(), SUM(), etc., within a GROUP BY query to perform
calculations on grouped data. However, it's important to note that not all SQL functions can be
used within a GROUP BY query. Only aggregate functions or functions that operate on
grouped data can be used effectively in this context.
Question 3
Assertion. SQL SELECT query can also fetch rows from multiple tables.
Reason. A join is a query that combines rows from two or more tables.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
SQL SELECT queries can fetch rows from multiple tables using JOIN operations. This allows
retrieving data from related tables simultaneously in a single query. A join in SQL is a query
that combines rows from two or more tables based on a specified condition, such as matching
values in a common column.
Question 4
Assertion. Generally, a join query combines rows from multiple tables having some common
column(s) and a joining condition, but not always.
Reason. A Cartesian product is an unrestricted join where all possible combinations of rows
from multiple tables are created without any condition on them.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
In SQL, a join is a query that combines rows from two or more tables based on a specified
condition, such as matching values in a common column. However, this matching condition is
not always required; for instance, a Cartesian product is an unrestricted join where all possible
combinations of rows from multiple tables are generated without any specific condition. This
means that every row from one table is combined with every row from another table.
Question 5
Assertion. The join, in which columns are compared for equality, is called Equi-join.
Reason. The join queries only produce combined rows from tables having some common
column, when compared for equality.
Answer
(c)
Explanation
The join, in which columns are compared for equality, is called Equi-join. Join queries in SQL
can produce combined rows based on various conditions, not just equality comparisons. For
example, join conditions can involve inequality comparisons (<, >, <=, >=), as well as other
logical operators and functions.
Question 6
Assertion. In the result produced by a join query, there may be multiple identical columns in
the final result, but not always.
Reason. The join in which only one of the identical columns (coming from the joined tables)
exists, is called a Natural join.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
In the result produced by a join query, there may be multiple identical columns in the final
result, but this is not always the case. Depending on the type of join and the columns involved,
duplicate columns may or may not appear in the result set. A Natural join is a type of join in
SQL where only one of the identical columns (coming from the joined tables) exists in the final
result.
Question 7
Assertion. In order to carry out the join operation, there should be a governing condition.
Reason. A join condition may be based on equality, less than, greater than or non-equality.
Answer
(d)
Explanation
In order to carry out the join operation, there should be a governing condition is false. This is
because a Cartesian join is an example of a join operation that does not require a specific
condition; it combines all rows from one table with all rows from another table without any
matching condition. A join condition may be based on equality, less than, greater than, or non-
equality depending on the requirements of the query.
Question 1
Answer
HAVING conditions are applicable to groups formed by WHERE conditions are applicable to
GROUP BY clause. individual rows.
It allows conditions to be applied to grouped data. It filters rows based on specified condition
Question 2
Answer
The GROUP BY clause in SQL is used to combine all records that have identical values in a
particular field or group of fields. This grouping results in one summary record per group if
group functions, such as aggregate functions, are used with it.
Question 3
What are aggregate functions? What is their use? Give some examples.
Answer
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 group.
Examples of aggregate functions include SUM(), COUNT(), MAX(), MIN(), AVG() etc.
Question 4
What type of functions can you use with GROUP BY and HAVING clauses ?
Answer
Aggregate functions are used with the GROUP BY and HAVING clauses to apply conditions on
grouped data.
Question 5
Answer
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 a comprehensive view of the information.
Question 6
Answer
1. Equi-Join
2. Non-Equi-Join
3. Natural join
Question 1
Table BOOK_INFORMATION
Column Name
BOOK_ID
BOOK_TITLE
PRICE
Which SQL statement allows you to find the highest price from the table
BOOK_INFORMATION?
Answer
Explanation
Question 2
Table SALES
Column Name
STORE_ID
SALES_DATE
SALES_AMOUNT
Which SQL statement lets you find the sales amount for each store?
Answer
Question 3
Table SALES
Column Name
STORE_ID
SALES_DATE
SALES_AMOUNT
Which SQL statement lets you list all stores whose total sales amount is over 5000 ?
Answer
Explanation
Question 4
Table SALES
Column Name
STORE_ID
SALES_DATE
SALES_AMOUNT
Which SQL statement lets you find the total number of stores in the SALES table?
Answer
Explanation
Question 5
Table SALES
Column Name
STORE_ID
SALES_DATE
SALES_AMOUNT
Which SQL statement allows you to find the total sales amount for Store ID 25 and the total
sales amount for Store ID 45?
Answer
Explanation
Question 6
Table EXAM_RESULTS
10 LAURA LYNCH 1 90
10 LAURA LYNCH 2 85
11 GRACE BROWN 1 78
11 GRACE BROWN 2 72
12 JAY JACKSON 1 95
12 JAY JACKSON 2 92
13 WILLIAM BISHOP 1 70
14 CHARLES PRADA 2 85
What SQL statement do we use to find the average exam score for EXAM_ID = 1?
Answer
Output
+-----------------+
| AVG(EXAM_SCORE) |
+-----------------+
| 83.2500 |
+-----------------+
Explanation
Question 7
Table EXAM_RESULTS
10 LAURA LYNCH 1 90
10 LAURA LYNCH 2 85
11 GRACE BROWN 1 78
11 GRACE BROWN 2 72
12 JAY JACKSON 1 95
12 JAY JACKSON 2 92
13 WILLIAM BISHOP 1 70
14 CHARLES PRADA 2 85
Which SQL statement do we use to find out how many students took each exam?
Output
+---------+------------------------+
| EXAM_ID | COUNT(DISTINCT STU_ID) |
+---------+------------------------+
| 1 | 4 |
| 2 | 5 |
+---------+------------------------+
Explanation
Question 8
Table EXAM_RESULTS
10 LAURA LYNCH 1 90
10 LAURA LYNCH 2 85
11 GRACE BROWN 1 78
11 GRACE BROWN 2 72
12 JAY JACKSON 1 95
12 JAY JACKSON 2 92
STU ID FNAME LNAME EXAM ID EXAM_SCORE
13 WILLIAM BISHOP 1 70
14 CHARLES PRADA 2 85
What SQL statement do we use to print out the record of all students whose last name starts
with 'L'?
Answer
Output
+--------+-------+-------+---------+------------+
| stu_id | fname | lname | exam_id | exam_score |
+--------+-------+-------+---------+------------+
| 10 | LAURA | LYNCH | 1 | 90 |
| 10 | LAURA | LYNCH | 2 | 85 |
+--------+-------+-------+---------+------------+
Explanation
1. SELECT * FROM EXAM_RESULTS WHERE LNAME LIKE 'L%'; — The LIKE operator is
used for pattern 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 * statement retrieves all columns from the
EXAM_RESULTS table for the matching records.
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, when using the LIKE operator in SQL for pattern
matching, we use wildcard characters (%) to represent unknown parts of a string.
3. SELECT * FROM EXAM_RESULTS WHERE LNAME 'L'; — This statement contains a
syntax error. In SQL, 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.
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.
Question 9
Table EXAM_RESULTS
STU ID FNAME LNAME EXAM ID EXAM_SCORE
10 LAURA LYNCH 1 90
10 LAURA LYNCH 2 85
11 GRACE BROWN 1 78
11 GRACE BROWN 2 72
12 JAY JACKSON 1 95
12 JAY JACKSON 2 92
13 WILLIAM BISHOP 1 70
14 CHARLES PRADA 2 85
1. 90
2. 85
3. 100
4. 95
Answer
Output
+-----------------+
| MAX(EXAM_SCORE) |
+-----------------+
| 95 |
+-----------------+
Explanation
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.
Question 10
Answer
1.
Output
+------------------------+
| COUNT(DISTINCT SPORTS) |
+------------------------+
| 4 |
+------------------------+
Explanation
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 offered by the club.
2.
Output
+----------+
| MIN(Age) |
+----------+
| 34 |
+----------+
Explanation
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.
3.
Output
+-----------+
| AVG(Pay) |
+-----------+
| 1100.0000 |
+-----------+
Explanation
The SQL query SELECT AVG(Pay) FROM CLUB WHERE Sports = 'KARATE' ; calculates
the average value of the '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.
Output
+----------+
| SUM(Pay) |
+----------+
| 7800 |
+----------+
Explanation
The SQL query SELECT SUM(Pay) FROM CLUB WHERE Dateofapp > '1998-01-
31'; calculates the sum of the '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
Table : EMPLOYEE
Table : JOB
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 1300000.
3. To display names and corresponding job titles of those employees who have 'SINGH'
(anywhere) in their names.
4. Identify foreign key in the table EMPLOYEE.
5. Write SQL command to change the JOBID to 104 of the EMPLOYEE with ID as E4 in
the table 'EMPLOYEE'.
Answer
1.
Output
+------------+-------------------+-------
+--------------------------+
| EMPLOYEEID | NAME | JOBID | JOBTITLE
|
+------------+-------------------+-------
+--------------------------+
| E1 | SUMIT SINHA | 102 | VICE PRESIDENT
|
| E2 | VIJAY SINGH TOMAR | 101 | PRESIDENT
|
| E3 | AJAY RAJPAL | 103 | ADMINISTARTION
ASSISTANT |
| E4 | MOHIT RAMNANI | 102 | VICE PRESIDENT
|
| E5 | SHAILJA SINGH | 103 | ADMINISTARTION
ASSISTANT |
+------------+-------------------+-------
+--------------------------+
2.
Output
+---------------+---------+--------------------------+
| NAME | SALES | JOBTITLE |
+---------------+---------+--------------------------+
| AJAY RAJPAL | 1400000 | ADMINISTARTION ASSISTANT |
| SHAILJA SINGH | 1450000 | ADMINISTARTION ASSISTANT |
+---------------+---------+--------------------------+
3.
Output
+-------------------+--------------------------+
| NAME | JOBTITLE |
+-------------------+--------------------------+
| VIJAY SINGH TOMAR | PRESIDENT |
| SHAILJA SINGH | ADMINISTARTION ASSISTANT |
+-------------------+--------------------------+
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.
5.
UPDATE EMPLOYEE
SET JOBID = 104
WHERE EMPLOYEEID = 'E4';
Output
Question 12
Consider the following tables Employee and Salary. Write SQL commands for the statements
(i) to (iv) and give outputs for SQL queries (v) to (vii)
Table : Employee
Table : Salary
Answer
1.
SELECT Name
FROM Employee
WHERE Name LIKE 'H%';
3.
UPDATE Salary
SET Total_Sal = Basic + `D.A.` + HRA + Bonus;
5.
SELECT MAX(Basic)
FROM Salary
WHERE Bonus > 40;
Output
+------------+
| MAX(Basic) |
+------------+
| 10000 |
+------------+
6.
SELECT Sec as sex, COUNT(*) AS Count
FROM Employee
GROUP BY Sec ;
Output
+-----+-------+
| sex | Count |
+-----+-------+
| F | 2 |
| M | 4 |
+-----+-------+
7.
Output
+-------+
| Depid |
+-------+
| 101 |
| 102 |
| 103 |
+-------+
Question 13
With reference to following relations PERSONAL and JOB answer the questions that follow :
Create following tables such that Empno and Sno are not null and unique, date of birth is after
'12-Jan-1960', name is never blank, Area and Native place is valid, hobby, dept is not empty,
salary is between 4000 and 10000.
Table : Personal
Table : Job
Sno Area App_date Salary Retd_date Dept
(a) Show empno, name and salary of those who have Sports as hobby.
(e) Show Sno, Name, Hobby and Salary in descending order of Salary.
(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.
(i) Show additional burden on the company in case salary of employees having hobby as
sports, is increased by 10%.
(k) Show how many employee shall retire today if maximum length of service is 20 years.
(l) Show those employee name and date of birth who have served more than 17 years as on
date.
(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 completed atleast 3 years of service.
(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.
(t) Erase the records of employee from Job table whose hobby is not Sports.
Answer
(a)
Output
+-------+--------+--------+
| EMPNO | NAME | Salary |
+-------+--------+--------+
| 125 | Vinod | 8500 |
| 129 | Ramesh | 7000 |
+-------+--------+--------+
(b)
SELECT name
FROM personal
WHERE dobirth = (
SELECT MIN(dobirth)
FROM personal
);
Output
+------+
| name |
+------+
| Amit |
+------+
(c)
Output
+-----------+----------------+
| Area | Employee_Count |
+-----------+----------------+
| Agra | 2 |
| Delhi | 1 |
| Mathura | 1 |
| Pune | 1 |
| Bangalore | 1 |
+-----------+----------------+
(d)
Output
+--------+--------------+------------+
| Name | Native-place | dobirth |
+--------+--------------+------------+
| Abhai | Allahabad | 1975-08-11 |
| Vinod | Delhi | 1977-04-04 |
| Manoj | Mumbai | 1976-12-12 |
| Ramesh | Pune | 1981-10-28 |
+--------+--------------+------------+
(e)
Output
+-----+--------+-----------+--------+
| SNO | NAME | HOBBY | SALARY |
+-----+--------+-----------+--------+
| 125 | Vinod | Sports | 8500 |
| 128 | Abhay | Gardening | 7500 |
| 129 | Ramesh | Sports | 7000 |
| 127 | Manoj | Writing | 6000 |
| 124 | Abhai | Music | 5500 |
| 123 | Amit | Music | 5000 |
+-----+--------+-----------+--------+
(f)
SELECT HOBBY
FROM PERSONAL
WHERE Name = 'abhay' or Name = 'abhai' ;
Output
+-----------+
| HOBBY |
+-----------+
| Music |
| Gardening |
+-----------+
(g)
Output
+------------+--------------+
| App_date | native-place |
+------------+--------------+
| 2006-01-25 | Delhi |
| 2007-08-19 | Allahabad |
| 2004-04-14 | Delhi |
| 2008-03-13 | Mumbai |
+------------+--------------+
(h)
Output
+----------------+
| Salary Expense |
+----------------+
| 5000 |
| 5500 |
| 8500 |
| 6000 |
| 7500 |
| 7000 |
+----------------+
(i)
Output
+-------------------+
| Additional Burden |
+-------------------+
| 1550.0 |
+-------------------+
(j)
SELECT Hobby
FROM PERSONAL
GROUP BY Hobby
HAVING COUNT(*) >= 2;
Output
+--------+
| Hobby |
+--------+
| Music |
| Sports |
+--------+
(k)
SELECT COUNT(*)
FROM Job
WHERE DATEDIFF(CURDATE(), App_date) >= 20 * 365;
Output
+----------+
| COUNT(*) |
+----------+
| 0 |
+----------+
(l)
Output
+-------+------------+
| Name | Dobirth |
+-------+------------+
| Amit | 1965-01-23 |
| Manoj | 1976-12-12 |
+-------+------------+
(m)
SELECT Name
FROM Personal, job
where personal.Empno = job.Sno
and job.Salary > ( select max(salary)
from job
where dept = 'sales');
Explanation
There will be no output because there are no employees whose salary is greater than the
highest salary in the Sales department.
(n)
Output
+-----+-----------+------------+--------+------------
+-----------+
| sno | area | app_date | salary | retd_date | dept
|
+-----+-----------+------------+--------+------------
+-----------+
| 123 | Agra | 2006-01-25 | 5250 | 2026-01-25 |
Marketing |
| 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
|
| 129 | Bangalore | 2003-07-21 | 7350 | 2023-07-21 |
Finance |
+-----+-----------+------------+--------+------------
+-----------+
(o)
1.
Output
+-----------+
| hobby |
+-----------+
| Music |
| Sports |
| Writing |
| Gardening |
+-----------+
2.
Output
+-------------+
| AVG(SALARY) |
+-------------+
| 6650.0000 |
+-------------+
3.
+--------------------------------+
| COUNT(DISTINCT `NATIVE-PLACE`) |
+--------------------------------+
| 4 |
+--------------------------------+
4.
Output
+------+-------------+
| name | max(salary) |
+------+-------------+
| Amit | 8500 |
+------+-------------+
Explanation
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, including a non-group field like 'name' in the select-list means it
will return the value from the first 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)
Output
Output
DELETE j
FROM Job j, Personal p
WHERE j.Sno = p.Empno AND p.Hobby <> 'Sports';
(u)
Question 14a
With reference to the table below, answer the questions that follow :
Table : Employees
Table : EmpSalary
Write the SQL commands for the following using above tables :
(i) To show firstname, lastname, address and city of all employees living in Pairs.
(iii) To display the firstname, lastname and total salary of all managers from the tables
Employes and EmpSalary, where total salary is calculated as Salary + Benefits.
(iv) To display the maximum salary among managers and clerks from the table EmpSalary.
Answer
(i)
Output
+-----------+----------+-------------+-------+
| Firstname | Lastname | Address | City |
+-----------+----------+-------------+-------+
| SAM | TONES | 33 ELM ST. | PARIS |
| PETER | THOMPSON | 11 RED ROAD | PARIS |
+-----------+----------+-------------+-------+
(ii)
SELECT *
FROM Employees
ORDER BY Firstname DESC;
Output
+-------+-----------+----------+-------------------
+------------+
| empid | FIRSTNAME | LASTNAME | ADDRESS | CITY
|
+-------+-----------+----------+-------------------
+------------+
| 215 | SARAH | ACKERMAN | 440 U.S. 110 | UPTON
|
| 152 | SAM | TONES | 33 ELM ST. | PARIS
|
| 300 | ROBERT | SAMUEL | 9 FIFTH CROSS |
WASHINGTON |
| 335 | RITU | TONDON | SHASTRI NAGAR | GZB
|
| 10 | RAVI | KUMAR | RAJ NAGAR | GZB
|
| 400 | RACHEL | LEE | 121 HARRISON ST. | NEW YORK
|
| 441 | PETER | THOMPSON | 11 RED ROAD | PARIS
|
| 244 | MANILA | SENGUPTA | 24 FRIENDS STREET | NEW
DELHI |
| 105 | HARRY | WALTOR | GANDHI NAGAR | GZB
|
+-------+-----------+----------+-------------------
+------------+
(iii)
Output
+-----------+----------+-------------+
| Firstname | Lastname | TotalSalary |
+-----------+----------+-------------+
| RAVI | KUMAR | 90000 |
| HARRY | WALTOR | 80000 |
| SARAH | ACKERMAN | 87500 |
+-----------+----------+-------------+
(iv)
Output
+-------------+-------------+
| Designation | MAX(Salary) |
+-------------+-------------+
| MANAGER | 75000 |
| CLERK | 50000 |
+-------------+-------------+
Question 14b
With reference to the table below, answer the questions that follow :
Table : Employees
(i) Select firstname, Salary from Employees, Empsalary where Designation = 'Salesman' and
Employees.Empid = Empsalary.Empid ;
(iii) Select designation, sum(salary) from EmpSalary group by designation having count(*) > 2 ;
Answer
(i)
Output
+-----------+--------+
| FIRSTNAME | SALARY |
+-----------+--------+
| RACHEL | 32000 |
| PETER | 28000 |
+-----------+--------+
(ii)
Output
+-----------------------------+
| COUNT(DISTINCT DESIGNATION) |
+-----------------------------+
| 4 |
+-----------------------------+
(iii)
Output
+-------------+-------------+
| DESIGNATION | SUM(SALARY) |
+-------------+-------------+
| MANAGER | 215000 |
| CLERK | 135000 |
+-------------+-------------+
(iv)
Output
+---------------+
| SUM(BENEFITS) |
+---------------+
| 32000 |
+---------------+
Question 15
Show the average salary for all departments with more than 3 people for a job.
Answer
Output
+----------+-----------+
| job | AvgSalary |
+----------+-----------+
| CLERK | 1037.5 |
| SALESMAN | 1400 |
+----------+-----------+
Question 16
Display only the jobs with maximum salary greater than or equal to 3000.
Answer
SELECT Job
FROM Empl
GROUP BY Job HAVING MAX(Sal) >= 3000;
Output
+-----------+
| Job |
+-----------+
| PRESIDENT |
| ANALYST |
+-----------+
Question 17
Answer
Output
+-------------+
| NumManagers |
+-------------+
| 3 |
+-------------+
Question 18
Answer
Output
+--------+----------------+
| deptno | employee_count |
+--------+----------------+
| 20 | 5 |
| 30 | 6 |
| 10 | 3 |
+--------+----------------+
Question 19
Answer
Output
+--------+--------------+
| deptno | total_salary |
+--------+--------------+
| 20 | 10885 |
| 30 | 9400 |
| 10 | 8750 |
+--------+--------------+
Question 20
Answer
Output
+--------+------------+
| deptno | max_salary |
+--------+------------+
| 20 | 3000 |
| 30 | 2850 |
| 10 | 5000 |
+--------+------------+
Question 21
Customers
customer id (PK)
first_name
last_name
address
city
state
zip
Orders
order id (PK)
order_date
amount
customer_id (FK)
Answer
Question 22
Customers
customer id (PK)
first_name
last_name
address
city
state
zip
Orders
order id (PK)
order_date
amount
customer_id (FK)
List the sum of the totals of orders grouped by customer and state.
Answer
Question 23
Customers
customer id (PK)
first_name
last_name
address
city
state
zip
Orders
order id (PK)
order_date
amount
customer_id (FK)
List the customers (name) and the total amount of all their orders.
Answer
Question 24
Answer
Question 25
Answer