You are on page 1of 6

MORE ON SQL – GROUPING RECORDS AND TABLE JOINS

Type A: Very Short Answer Questions


1 What are single row functions?
Ans. Single row functions work with a single row at a time. A single row function returns a result for every row of a
queried table.
2 What are multiple row functions?
Ans. Multiple row functions work with data of multiple rows at a time and return aggregated value.
3 What will be the output of following code?
mysql> SELECT COUNT (‘Inform’, ‘atics’);
Ans. Output of above code will be ‘ERROR’
4 What is Cartesian product? How is it related to join?
Ans. Joining two or more tables without any condition, it is called Cartesian product or Unrestricted Join.
5 What is join? How is natural join different from an equi-join?
Ans. A join is a query that combines rows from two or more tables.
Natural Join Equi Join
Natural join is a type of equi join which occurs Equi join is a special type of join in which we use only
implicitly by comparing all the same names columns equality operator. Hence, when you make a query for
join using equality operator then that join query comes
in both tables. The join results have only one column
under Equi join.
for each pair of equally named columns.
6 What is table alias? What is the purpose of table alias?
Ans. A table alias is a temporary label given along with table name in from clause.
A table alias can be used anywhere in the SELECT statement.
7 Define an equi-join. What is non-equi-join?
Ans. In an equi-join, the values in the columns being joined are compared for equality. All columns in the tables being
joined are included in the results. Here, all columns from joining table appear in the output even if they are
identical.
Non-equi-join is a query that specifies some relationship other than equality between the columns.
8 What is join? How many different types of join can you create in MySQL?
Ans. A join is a query that combines rows from two or more tables.
1. Equi join
2. Non-equi join
3. Natural join
4. Cross join
5. Left join
6. Right join
9 There are multiple ways to create Cartesian product of two tables in MYSQL. Describe them.
Ans. Following methods can be used to create Cartesian products of two table –
1. Unrestricted Join – SELECT * FROM table1, table2 will give Cartesian product because where condition is not
given in the query.
2. Join – SELECT * FROM table1 JOIN table2 will give Cartesian product because no join-condition is specified.
3. Cross Join – SELECT * FROM table1 CROSS JOIN table2 will also give Cartesian Product this join simply matches
each row from one table to every row from another table.
10 How is a left join different from a natural join?
Ans. Natural join, shows only the matched rows of both the tables whereas left join display all rows of the first table
regardless they are matched or not and also display the unmatched rows of first table as NULL.
11 How is cross join different from natural join?
Ans. Natural join is the result obtained when the identical columns are eliminated. Whereas Cross join is a basic type
of join that simply matches each row from one table to every row from another table.
12 Can you join two tables without using the keyword JOIN?
Ans. YES, two tables can be joined without using JOIN keyword by simply using equal sign ‘=’.

Page 1 of 6
13 What is difference between ON and USING join-clause?
Ans. The difference between ON and USING sub-clauses of JOIN clause of SELECT is that ON clause requires a
complete-join condition whereas USING clause requires just the name of a join-field. USING sub clause produces
natural join whereas ON clause produces equi-join.
14 A table STUDENT has 4 rows and 2 columns and another table TEACHER has 3 row and 4 columns. How many
rows and columns will be there if we obtain the Cartesian product of these two tables?
Ans. 12 rows and 6 columns
TYPE B: Short Answer Questions
1 Given the following table:

Give the output of following SQL statements:


(i) SELECT COUNT (DISTINCT SPORTS) FROM Club;
(ii) SELECT MIN(Age) FROM CLUB WHERE Sex=’F’;
(iii) SELECT AVG(Pay) FROM CLUB WHERE Sports = ‘KARATE’;
(iv) SELECT SUM(Pay) FROM CLUB WHERE Datofapp > ‘31/01/98’;
Ans. (i) 4
(ii) 34
(iii) 1100
(iv) 9800
2 Given the following table:
TABLE: STUDENT

Give the output of following SQL statements:


(i) SELECT MIN(AvgMark) FROM STUDENT WHERE AvgMark<75;
(ii) SELECT SUM(Stipend) FROM Student WHERE Grade=’B’;
(iii) SELECT AVG(Stipend) FROM Student WHERE Class=’12A’;
(iv) SELECT COUNT(DISTINCT) FROM Student;
Ans. (i) 64.4
(ii) 1150
(iii)

Page 2 of 6
3 Given the following table:

Give the output of following SQL commands on the basis of table Library.
(i) SELECT MIN(Price) FROM Library WHERE Price<150;
(ii) SELECT AVG(Price) FROM Library WHERE QTY<3;
(iii) SELECT COUNT(DISTINCT) FROM Library;
Ans.
4 Given the following table:

(i) SELECT AVG(Price) FROM Mov WHERE Price<30;


(ii) SELECT MAX(Price) FROM Mov WHERE Price>30;
(iii) SELECT SUM(Price*Qty) FROM Mov WHERE QTY<4;
(iv) SELECT COUNT(DISTINCT Type) FROM Mov
Ans. (i) 26.61
(ii) 69.95
(iii) 793.10
(iv) 4
5 Show the average salary for all departments with more than 3 people for a job.
Ans. select avg(salary) from departments where job>3;
6 Display only the jobs with maximum salary greater than or equal to 3000.
Ans. select max(job) from departments where salary>=3000;
7 Find out number of employees having “Manager” as Job.
Ans. 1.
8 List the count of employees grouped by deptno. (table EMPL)
Ans.
9 List the sum of employees’ salaries grouped by department. (table EMPL)
Ans.
10 List the maximum salary of employee grouped by their department number.
Ans.
Consider the tables Customers, Parts and Orders given in solved problems. Answer Questions 11 to 16 based on
these:
11 List the total of customers’ orders grouped by customer (id).
Ans.

Page 3 of 6
12 List all customers (name)who have orders (use EXISTS).
Ans.
13 List the sum of the totals of orders grouped by customer and state.
Ans.
14 List the sum of the totals of orders where this sum is greater than $1000 grouped by customer (id) and state and
ordered by state.
Ans.
15 List the customers (name) and their orders’ details.
Ans.
16 List the customers (name) and the total amount of all their orders.
Ans.
Consider tables EMPL, Dept, SalaryGrade. The Empl table has already been listed in earlier chapters. Schemas of
tables SalaryGrade and Dept are being shown below:
SALARYGRADE (Lowsal, HIghsal, Grade)
Dept (Deptno, DeptName, Location)
Answer Questions 17 and 18 on basis of these tables:
17 List the department names and the number of their employees.
Ans. select dname,empno from dept,empl where empl.deptno=dept.deptno;
18 List the employee names and the name of their departments.
Ans. select ename, dname from empl, dept where empl.deptno=dept.deptno;
19 What is join? How many different types of joins can you create in MySQL.
Ans.
20 There are multiple ways to create Cartesian product of two tables in MySQL. Describe them.
Ans.
21 How is a left join different from a natural join? Give Example.
Ans.
22 How is cross join different from natural join? Give Example.

23 In a database there are two tables:

Write MySql queries for the following:


(i) To display ICode,IName and corresponding Brand of those Items, whose price is between 20000 and

Page 4 of 6
45000 (both values inclusive).
(ii) To display ICode, Price and BName of the item which has IName as “Television”?
(iii) To increase the price of all the Items by 15%.
Ans. (i) Select Item.ICode,IName,Brand.Brand from Item,Brand where item.icode=brand.icode and Item.Price
Between 20000 AND 45000;
(ii) Select Item.ICode,Price,Brand.Brand from Item,Brand where item.icode=brand.icode and Item.Iname Like
'Television';
(iii) UPDATE ITEM SET Price=Price+(Price*15/100);
24 Given below is a Table Patient.

(i) Identify Primary Key in the table given above.


(ii) Write MySql query to add a column Department with data type varchar and size 30 in the table
Patient.
Ans. (i) P_No
(ii) ALTER TABLE Patient ADD(Department varchar(30));
25 A table “TRAINS” in a database has degree 3 and cardinality 8. What is the number of rows and columns in it?

8
3

26 In a database there are two tables “Product” and “Client” as shown below:

Write the command in SQL queries for the following:


(i) To display the details of Product whose Price is I the range of 40 and 120(Both values included) (1)

Page 5 of 6
(ii) To display the ClientName, City from table Client and ProductName and Price from table Product, with
their corresponding matching P_ID. (2)
(iii) To increase the price of all the products by 20. (2)
Ans. (i) SELECT * FROM Product WHERE Price BETWEEN 40 AND 120;
(ii) SELECT Clientname, City, Productname, price, P_ID FROM Product, Client WHERE Client.P_ID =
Product.P_ID;
(iii) UPDATE Product
SET Price = Price + 20;
27 In a database School there are two tables Member and Division as show below.

(i) Identify the foreign key in the table Member.


(ii) What output, you will get, when an equi-join query is executed to get the NAME from Member Table and
corresponding from Division table?
(i) Divno
(ii)
Name Divname
Shankhya Media
Sunish Dance

Page 6 of 6

You might also like