0% found this document useful (0 votes)
7 views

Sqlboard Based Practice Questions for

The document provides an overview of SQL concepts, including definitions of key terms such as primary key, foreign key, and various SQL commands like DDL and DML. It also includes multiple-choice questions and fill-in-the-blank exercises related to SQL syntax and database management. The content is designed to test knowledge and understanding of SQL and relational database principles.

Uploaded by

mohantysreetam60
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Sqlboard Based Practice Questions for

The document provides an overview of SQL concepts, including definitions of key terms such as primary key, foreign key, and various SQL commands like DDL and DML. It also includes multiple-choice questions and fill-in-the-blank exercises related to SQL syntax and database management. The content is designed to test knowledge and understanding of SQL and relational database principles.

Uploaded by

mohantysreetam60
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Database Query using SQL

Glimpses
Candidate All the attributes combinations inside a relation that can serve as primary key.
Key

Constraint Rule and conditions set for data being stored in a database.

DDL Data Definition Language. SQL part-language that facilitates defining creation/
modification etc. of database objects such as tables, indexes, sequences etc.

DML Data Manipulation Language. SQL part-language that facilitates manipulation (addition/
deletion/ modification) of data residing in database object.

Equi Join A Join formed by equality of common field of two or more tables and where common
fields from all the tables appear in the final result.

Foreign Key A non key attribute whose values are derived from the primary key of some other tables.

Join A way to combine records coming from multiple tables having common fields.

Natural Join A type of equi-join where common column from the joining tables appears once only.

Primary Key A set of one or more attributes that can uniquely identify tuples within the relation.

Relation A table having non-empty atomic values with unordered rows and columns is relation.

SQL Structured Query Language. A non-procedural UGL used for querying upon relational
database.

Tuple A row in a relation is called tuple.

View A virtual table that does not really exist in its own right but is instead derived from one or
more underlying base tables in called a view.
OBJECTIVE TYPE QUESTIONS
Multiple Choice Questions
1. A is a property of the entire relation, which ensures through its value that each tuple 1
is unique in a relation.
(a) Rows (b) Key (c) Attributes (d) Fields

2. A relational database can have how many type of keys in a table ? 1


(a) Candidate Key (b) Primary Key (c) Foreign Key (d) All of these

3. Which one of the following uniquely identifies the tuples / rows in a relation. 1
(a) Secondary Key (b) Primary Key (c) Composite Key (d) Foreign Key

4. The Primary key is selected from the set of . 1


(a) Composite Key (b) Determinants (c) Candidates Key (d) Foreign Key

5. Which of the following is a group of one or more attributes that uniquely identifies a row? 1
(a) Key (b) Determinant (c) Tuple(d) Relation

6. Which of the following attributes cannot be considered as a choice for Primary Key ? 1
(a) Id(b) License number (c) Dept_Id (d) Street

7. An attribute in a relation is a foreign key if it is the key in any other relation. 1


(a) Candidate (b) Primary (c) Super (d) Sub

8. Consider the table with structure as : 1


Student(ID, name, dept_name, tot_cred)
In the above table, which attribute will form the primary key?
(a) name (b) dept_name(c) Total_credits (d) ID

9. Which of the following is not a legal sub-language of SQL ? 1


(a) DDL (b) QAL(c) DML (d) TCL

10. Which of the following is a DDL command? 1


(a) SELECT (b) ALTER (c) INSERT (d) UPDATE

11. In SQL, which of the following will select only one copy of each set of duplicate rows
from a table.
(a) SELECT UNIQUE
(b) SELECT DISTINCT
(c) SELECT DIFFERENT
(d) All of these.

12. Which of the following keywords will you use in the following query to display the unique 1
values of the column dept_name?
SELECT dept_name FROM COMPANY;
(a) All (b) From(c) Distinct (d) Name

13. The clause of SELECT query allows us to select only those rows in the result that 1
satisfy a specified condition.
(a) where (b) from (c) having (d) like
14. Which operator can take wild card characters for query condition? 1
(a) BETWEN (b) LIKE (c) IN (d) NOT

15. Which operator checks a value against a range of values? 1


(a) BETWEEN (b) LIKE (c) IN (d) NOT

16. Which of the following SQL commands retrives data from table(s) ? 1
(a) UPDATE (b) SELECT (c) Union (d) All of these

17. Which of the following queries contains an error ? 1


(a) Select * from emp where empid=10003;
(b) Select empid from emp where empid=10006;
(c) Select empid from emp;
(d) Select empid where empid=10009 and lastname= ‘GUPTA’;

18. Consider the following table namely Employee : 1

Employee_id Name Salary


1001 Misha 6000
1009 Khushi 4500
1018 Japneet 7000
Which of the names will not be displayed by the below given query ?
SELECT name from Employee WHERE employee_id>1009;

(a) Misha, Khushi (b) Khushi, Japneet (c) Japneet (d)Misha, Japneet

19. Which operator perform pattern matching ? 1


(a) BETWEN (b) LIKE (c) IN (d) NOT

20. Consider the following query 1


SELECT name FROM class WHERE Subject LIKE ‘ Informatics Practices’;
Which one of the following has to be added into the blank space to select the subject which
has informatics practices as its ending string?
(a) $(b) _ (c) ||(d) %

21. Which operator tests a column for the absence of data(i.e. NULL value) ? 1
(a) Exist Operator (b) NOT Operator (c) IS Operator (d) None of these

22. Which clause is used to sort the query result ? 1


(a) Order By (b) Sort By (c) Group By (d) Arrange By

23. By default ORDER BY clause list the result in order. 1


(a) Descending (b) Any(c) Same (d) Ascending

24. Consider the following query 1


SELECT * FROM employee ORDER BY salary , name ;
To display the salary from greater to smaller and name in alphabetical order which of the
following options should be used ?

(a) Ascending, Descending


(b) Asc, Desc
(c) Desc, Asc
(d) Descending, Ascending
25. What is the meaning of Remark LIKE “%5%5%”; 1
(a) Column Remark begin with two 5s
(b) Column Remark ends with two 5s
(c) Column Remark has more than two 5s
(d) Column Remark has two 5s in it, at any position

26. In SQL, which command(s) is/are used to change a table’s structure/characteristics? 1


(a) ALTER TABLE (b) MODIFY TABLE (c) CHANGE TABLE (d) All of these

27. Which of the following is/are the DDL Statement ? 1


(a) Create (b) Drop (c) Alter (d) All of these

28. A Table can have 1


(a) Many primary keys and many unique keys.
(b) One primary key and one unique key
(c) One primary key and many unique keys.
(d) Many primary keys and one unique key.

29. Which of the following types of table constraints will prevent the entry of duplicate rows? 1
(a) Unique (b) Distinct (c) Primary Key (d) Null

30. Consider the following SQL Statement. What type of statement is this ? 1
INSERT INTO instructor VALUES (10211, ‘SHREYA’ , ‘BIOLOGY’, 69000);
(a) Procedure (b) DML (c) DCL (d) DDL

31. Which of the following statements will delete all rows in a table namely mytablewithout 1
deleting the table’s structure.
(a) DELETE FROM mytable;’
(b) DELETE TABLE mytable;
(c) DROP TABLE mytable;
(d) None of these.

32. Which of the following query will drop a column from a table ? 1
(a) DELETE COLUMN column_name;
(b) DROP COLUMN column_name;
(c) ALTER TABLE table_name DROP COLUMN column_name;
(d) None of these

33. Logical operator used in SQL are: 1


(a) AND, OR, NOT (b) &&, ||, ! (c) $,|,! (d) None of these

34. Which of the following requirement can be implemented using a CHECK constraint? 1
(a) Student must be greater than 18 years old.
(b) Student must be form a BRICS Country (Brazil, Russia, India, China, South
Africa)
(c) Student’s roll number must exist in another table(say, namely Eligible)
(d) None of these
35. An attribute in a relation is termed as a foreign key when it reference the of another 1
relation.
(a) Foreign Key (b) Primary Key (c) Unique Key (d) Check Constraint

36. Data integrity constraints are used to : 1


(a) Control the access and rights for the table data.
(b) Ensure the entry of unique records in a table.
(c) Ensure the correctness of the data entered in the table as per some rule or condition
etc.
(d) Make data safe from accidental changes.

37. A relationship is formed via that relates two tables where one table references 1
other table’s key.
(a) Candidate Key (b) Primary Key (c) Foreign Key (d) Check Constraint

38. What is the maximum value that can be stored in NUMBERIC(4,2)? 1


(a) 9999.99 (b) 99.9999 (c) 99.99 (d) 9.99

39. What should be the data type for the column Pricestoring values less than Rs.1000 e.g. 1
200.21
(a) VARCHAR(50) (b) NUMBER (c) NUMBER(5,2) (d) NUMBER(6)

40. What is anamein the following SQL Statement ? 1


SELECT aname FROM table1 UNION SELECT aname FROM table2;
(a) row name (b) column Name (c) table name (d) database name

41. Data manipulation language (DML) includes statements that modify the of the 1
tables of database.
(a) Structure (b) Data (c) User (d) Size

42. All aggregate functions ignore NULLs except for the function. 1
(a) Distinct (b) Count(*) (c) Average() (d) None of these

43. Which of the following are correct aggregate functions in SQL 1


(a) AVERAGE() (b) MAX() (c) COUNT() (d) TOTAL()

44. Identify the correct INSERT queries from the following : 1


(a) INSERT INTO Persons(‘xxx1’, ‘yyy1’);
(b) INSERT INTO Persons(LastName, FirstName)
Values (‘xxx’, ‘yyy’);
(c) INSERT INTO Persons Values(‘xxx1’ , ‘yyy1’);
(d) INSERT INTO Persons Value(‘xxx1’ , ‘yyy1’);

45. Aggregate functions can be used in the select list or the clause of the select statement. 1
They cannot be used in a clause.
(a) Where, having (b) Having, where (c) Group by, having (d) Group by where

46. What is the meaning of “HAVING” clause in SELECT query. 1


(a) To filter out the summary groups.
(b) To filter out the column groups.
(c) To filter out the row and column values.
(d) None of the mentioned.

47. Which of the following is not a text function? 1


(a) TRIM () (b) TRUNCATE() (c) LEFT() (d) MID ()

48. What will be returned by the given query ? 1


SELECT INSTR(‘INDIA’, ‘DI’);
(a) 2 (b) 3 (c) -2 (d) -3

49. What will be returned by the given query ? 1


SELECT ROUND(153.669,2);
(a) 153.6 (b) 153.66 (c) 153.67 (d) 153.7

50. What will be returned by the given query? 1


SELECT month(‘2020-05-11’);
(a) 5 (b) 11 (c) May (d) November
Fill in the Blanks
1. The SQL keyword is used to specify the table(s) that contains the data to be 1
retrieved.

2. The command of SQL lets you make queries to fetch data from tables. 1

3. To remove duplicate rows from the result of a query, specify the SQL qualifier in select 1
list.

4. To obtain all columns, use a(n) instead of listing all the column names in the select 1
list.

5. The SQL clause contains the condition that specifies which rows are to be selected. 1

6. The SQL keyword is used in SQL expressions to select records based on patterns. 1

7. The operator is used for making range checks in SELECT queries. 1

8. The null values in a column can be searched for in a table using in the WHERE 1
clause of SELECT query.

9. To sort the rows of the result table, the clause is specified.

10. Columns can be sorted in descending sequence by using the SQL keyword . 1

11. By default, ORDER BY clause lists the records in order. 1

12. A database can be opened with <database> command. 1

13. command is used to create new relations in a database 1

14. A is a condition or check applicable on a field or set of fields. 1

15. The constraint creates a foreign key. 1

16. To define a column as a primary key, constraint is used in CREATE 1


TABLE.

17. is used to insert data in an existing table. 1

18. Rows of a table can be deleted using command. 1

19. To increase the size of a column in an existing table, use commond . 1

20. command removes a table from a database permanently. 1

21. command is used to alter the definition of already created table. 1


22. To remove table data as well table structure, use command 1

23. Use command to add new columns in an existing table. 1

24. A column added via ALTER TABLE command initially contains value for all rows. 1

25. Issue command to make changes to table permanent. 1

26. The clause is used to divide result of SELECT query in groups. 1

27. To specify condition with a GROUP BY clause, _ clause is used. 1

28. Only functions are used with GROUP BY clause. 1

29. Nested grouping can be done by providing in the GROUP BY expression. 1

30. The clause is used in SELECT queries to specify filtering condition for groups. 1

31. Aggregate Functions cannot be used in clause of the Select query. 1

32. The SQL built-in function total values in numeric columns. 1

33. The SQL built-in function computes the average of values in numeric columns. 1

34. The SQL built-in function obtains the largest value in a in numeric columns. 1

35. The SQL built-in function obtains the smallest value in a in numeric columns. 1

36. The SQL built-in function computes the number of rows in a table. 1

37. The functions that work with one row at a time are functions. 1

38. To compare an aggregate value in a condition, clause is used. 1

39. In equi-join, the join condition joins the two tables using operator 1

40. To get a substring of a string other than Substr() function is also used. 1

41. To get the day part of a date function is used. 1

42. To get day name from a date function is used. 1

43. To remove a character from the right side of a string, function is used 1

44. To get the current date, _ function is used. 1

45. An SQL clause combines records from two or more tables in database. 1

46. An is specific type of join that uses only equality comparisons in the join-condition. 1
7. join select all data starting from the left table and matching rows in the right table. 1

8. join is a reversed version of left join. 1

9. join produces a data set that includes only those rows from the left table which have 1
matching rows from the right table.

0. The avg() function in MySQL is an example of function. 1

ANSWER
1 (b) Key 2 (d) All of these 3 (b) Primary Key 4 (c) Candidate Key
5 (a) Key 6 (d) Street 7 (b) Primary 8 (d) ID
9 (b) QAL 10 (b) ALTER 11 (b) SELECT 12 (c) Distinct
DISTINCT
13 (a) where 14 (b) LIKE 15 (a) BETWEEN 16 (b) SELECT
17 (d) 18 (a) Misha, Khushi 19 (b) LIKE 20 (d) %
21 (c) IS Operator 22 (a) Order By 23 (d) Ascending 24 (c) Desc, Asc
25 (d) 26 (a) ALTER 27 (d) All of these 28 (c)
TABLE
29 (a) Unique 30 (b) DML 31 (a) 32 (c)
33 (a) 34 (a) and (c) 35 (b) Primary Key 36 (c)
37 (b)Foreign Key 38 (c)99.99 39 (c) NUMBER(5,2) 40 (b) column Name
41 (b) Data 42 (b)Count(*) 43 (b)And (c) 44 (b)And (c)
45 (c) Group by, 46 (a) 47 (b) TRUNCATE() 48 (b) 3
having
49 (c) 153.67 50 (a) 5

Fill in the Blanks


1 FROM 2 SELECT 3 DISTINCT 4 ASTRIK(*)
5 WHERE 6 LIKE 7 BETWEEN 8 IS NULL
9 ORDER BY 10 DESC 11 ASCENDING 12 USE
13 CREATE TABLE 14 CONSTRAINT 15 REFERENCES 16 PRIMARY KEY
17 INSERT INTO 18 DELETE 19 ALTER TABLE 20 DROP TABLE
21 ALTER TABLE 22 DROP TABLE 23 ALTER TABLE 24 NULL
25 COMMIT 26 GROUP BY 27 HAVING 28 AGGREGATE
29 MULTIPLE 30 HAVING 31 WHERE 32 SUM()
FIELDS
33 AVG() 34 MAX() 35 MIN() 36 COUNT()
37 SINGLE ROW 38 HAVING 39 = 40 MID()
41 DAY() 42 DAYNAME() 43 RTRIM() 44 CURDATE()
45 JOIN 46 EQUI-JOIN 47 LEFT JOIN 48 RIGHT JOIN
49 INNER 50 AGGREGATE
FUNCTION

Very Short Questions


1. What is a primary key? 1

2. What is a unique key ? It is a Primary key? 1

3. How many primary key and unique keys can be there in a table? 1

4. What is a foreign key? 1

5. What is a composite primary key? 1

6. What is a tuple 1

7. Give some examples of DDL commands. 1

8. What is DML ? 1
10 | P a g e
9. Give some examples of DML commands. Or Write the name of any two DML Commands of
SQL ?

10. In SQL,write the query to display the list of tables stroe in database. 1

11. What are constraints? 1

12. Give some examples of integrity constraints. 1

13. What is the role of NOT NULL constraint? 1

14. What is the role of UNIQUE constraints ? 1

15. What is the role of FOREIGN KEY constraints? 1

16. An NULL values are same as a zero or a blank space? 1

17. What will the SELECT ALL command do? 1

18. What is wrong with following statement ? 1


SELECT * FROM Employee
WHERE grade = NULL;
Write the corrected form of the above SQL Statement.

19. Which SQL aggregate function is used to count all records of a table? 1

ANSWERS
1. A Primary key is a field or a combination of fields that can uniquely identify a row/tuple in a 1
table/relation.

2. A Unique key in table/relation is any non-primary key field which also stores unique values 1
for each row just like a primary-key does. But only one key is designated as primary key. So
unique refers to a unique non-key field of a table.

3. There can be multiple unique keys in a table but there can be only primary key in a table. 1

4. A foreign key is a field of a table which is the primary key of another table through a foreign 1
key a relationship is established between two tables.

5. If a table / relation has a primary key which is a combination of multiple columns of a table, 1
it is known as a composite primary key.

6. A tuple refers to a row of relation. 1

7. CREATE, ALTER, DROP 1

8. DML refers to the Data Manipulation Language component of SQL. The DML commands 1
are used to manipulate and query upon the data stored in various tables of a database.

11 | P a g e
9. INSERT, UPDATE, SELECT and DELETE 1

10. SHOW TABLES; 1

11. Constraints are the rules or conditions imposed on various attributes of a table in a database 1
so that only the data that satisfy these rules and conditions can get stored in the data table.

12. NOT NULL 1


PRIMARY KEY
UNIQUE
CHECK

13 It indicates that in the data being inserted, the column must have some value and can not be 1
left NULL.

14 This constraint ensures that for an attribute there will be unique value for each row and no
value is being repeated in any other row for that attribute.

15 This constraint is used to ensure the referential integrity of data in the table. It matches the 1
value of the column designated as the foreign key in one table with another table’s Primary
key.

16 No, A Null value is not the same aa zero or a blank space. A zero is a legal numeric value 1
and blank space is legal character value, whereas NULL is a legal empty value that cannot
be accessed or compared with other values.

17 The SELECT ALL command will fetch all the rows from a table as per the defined commands. 1

18 A relation operator ‘=’ is not used with the NULL clause. 1


The Corrected form is :
SELECT * FROM Employee
WHERE grade is NULL;

19. COUNT(*)

SHORT ANSWER QUESTIONS


1. Explain each of the following with illustrations using a table 3
(i) Candidate Key (ii) Primary Key (iii) Foreign Key

12 | P a g e
2. Observe the following tables TRANSACTIONS and CUSTOMERS carefully and answer the 2
questions that follows :
Table : Transaction

TNo Type Amount CNo


T1 CREDIT 1000 C3
T2 DEBIT 1500 C1

Table : Customer
CNo CNAME
C1 ZEESHAN
C2 AMAN
C3 JASPREET

(i) What is the degree of the table Transaction ? what is the cardinality of the table
Customers ?
(ii) Identify the primary key and candidate keys from the table Transcations.

3. Are count(*) and count(<column-name>) the same functions? Why/ Why not? 2

4. Identify the problem/issue with the following SQL query : 2


SELECT house, count(*)
FROM student;

7. Anjali writes the following commands with respect to a table employee having fields, empno, 2
name, department, commission.
Command1: Select count(*) from employee;
Command2 :Select count(commission) from employee;
She gets the output 4 for the first command but get an output 3 for the second command.
Explain the output with justification.

8. Gopi Krishna is using a table Employee. It has the following columns : 2


Code, Name, Salary, Deptcode
He wants to display maximum salary departmentwise. He wrote the following command :
SELECT Deptcode, Max(Salary) FROM Employee;
But he did not get the desired result.
Rewrite the above query with necessary changes to help him get the desired output.

13 | P a g e
9. A relation Vehicles is given below : 3
Vno Type Company Price Qty
AW125 Wagon Maruti 250000 25
J0083 Jeep Mahindra 4000000 15
S9090 SUV Mitsubishi 2500000 18
M0892 Mini van Datsun 1500000 26
W9760 SUV Maruti 2500000 18
R2409 Mini van Mahindra 350000 15
Write SQL Commands to :
(a) Display the average price of each type of vehicle having quantity more than 20.
(b) Count the type of vehicles manufactured by each company.
(c) Display the total price of all the types of vehicles.

10. Shanya Khanna is using a table Employee. It has the following columns : 2
Admno, Name, Agg, Stream
[Column Agg contain Aggregate marks]
She wants to display the highest Agg obtain in each Stream.
She wrote the following statement:
SELECT Stream, Max(Agg) FROM Employee;
But she did not get the desired result.
Rewrite the above query with necessary changes to help her get the desired output.

11. Write SQL queries for (i) to (iii), which are based on the following table PARTICIPANTS: 3
PNO EVENT SNAME CLASS DOB
P1 DEBATE SANYAM 12 2001-12-25
P2 DEBATE SHRUTI 10 2003-11-10
P3 DEBATE MEHER 12 2001-11-10
P4 QUIZ SAKSHI 11 2002-10-12
P5 QUIZ RITESH 12 2001-10-12
P6 QUIZ RAHUL 10 2003-10-12
P7 CROSSWORD AMEER 11 2002-05-09
P8 CROSSWORD MINAKSHI 12 2001-05-09
(i) To display details of all PARTICIPANTS of class 10 and 12.
(ii) To display the SNAME and Class of all PARTICIPANTS in ascending order of
their SNAME.
(iii) To display the number of PARTICIPANTS along with their respective CLASS, of
every CLASS.

12. Write ouputs for SQL queries (i) to (iii), which are based on the following tables 3
CUSTOMERS and PURCHASES :
Table : CUSTOMERS
CNO CNAME CITIES
C1 SANYAM DELHI
C2 SHRUTI DELHI
C3 MEHER MUMBAI
C4 SAKSHI CHENNAI
C5 RITESH INDORE
C6 RAHUL DELHI
C7 AMEER CHENNAI
C8 MINAKSHI BANGLORE
C9 ANSHUL MUMBAI

14 | P a g e
Table : PURCHASES
SNO QTY PUR_DATE CNO
S1 15 2018-12-25 C2
S2 10 2018-11-10 C1
S3 12 208-11-10 C4
S4 7 2019-01-12 C7
S5 11 2019-02-12 C2
S6 10 2018-10-12 C6
S7 5 2019-05-09 C8
S8 20 2019-05-09 C3
S9 8 208-05-09 C9
S10 15 2018-11-12 C5
S11 6 2018-08-04 C7

(i) SELECT COUNT(DISTINCT CITIES) FROM CUSTOMERS;


(ii) SELECT MAX(PUR_DATE) FROM PURCHASES;
(iii) SELECT CNAME,QTY, PUR_DATE FROM CUSTOMERS, PURCHASES
WHERE CUSTOMERS.CNO=PURCHASES.CNO AND QTY IN (10,20);

13. Write SQL queries for (i) to 9iv), which are based on the tables : CUSTOMERS and 3
PURCHASES given in above. /
(i) To Display details of all CUSTOMERS whose CITIES are neither Delhi nor 4
Mumbai.
(ii) To Display the CNAME and CITIES of all CUSTOMERS in ascending order of
their CNAME.
(iii) To Display the number of CUSTOMERS along with their respective CITITES in
each of the CITITES.
(iv) To Display details of all PURCHASES whose Quantity is more than 15.

14. Consider the following table ACTIVITY and COACH and answer the following parts of this 4
question : Table : ACTIVITY

Acode ActivityName Stadium ParticipantsNu PrizeMone ScheduleDate


m y
1001 Relay 100 x 4 Star Annex 16 10000 23-Jan-04
1002 High Jump Star Annex 10 12000 12-Dec-03
1003 Shot Put Super 12 8000 14-Feb-04
Power
1005 Long Jump Star Annex 12 9000 01-Jan-04
1008 Discuss Throw Super 10 15000 19-Mar-04
Power

Table :COACH
PCode Name ACode
1 Ahmad Hussain 1001
2 Ravinder 1008
3 Janila 1001

15 | P a g e
4 Naaz 1003
Give the output of the following SQL queries:
(i) SELECT COUNT(DISTINCT ParticipantsNum) FROM ACTIVITY;
(ii) SELECT MAX(ScheduleDate), MIN(ScheduleDate) FROM ACTIVITY;
(iii) SELECT Name,ActivityName FROM ACTIVITY A, COACH C
WHERE A.Acde=C.Acode AND A.ParticipantsNum=10;
(iv) SELECT DISTINCT ParticipantsNum FROM ACTIVITY;

15. Consider the following table STOCK and DEALERS and answer the following parts of this 4
question :
Table :STOCK

ItemNo Item Dcode Qty UnitPrice StockDate


5005 Ball Pen 0.5 102 100 16 31-Mar-10
5003 Bal Pen 0.25 102 150 20 01-Jan-10
5002 Gel Pen Premium 101 125 14 14-Feb-10
5006 Gel Pen Classis 101 200 22 01-Jan-09
5001 Eraser Small 102 210 5 19-Mar-09
5004 Eraser Big 102 60 10 12-Dec-09
5009 Sharpener Classis 103 160 8 23-Jan-09

Table :DEALERS

Dcode Dname
101 Reliable Stationers
103 Classis Plastics
102 Clear Deals

Give the output of the following SQL queries:


(i) SELECT COUNT(DISTINCT Dcode) FROM STOCK;
(ii) SELECT QTY*UnitPrice FROM STOCK
WHERE ItemNo=5006;
(iii) SELECT Item, Dname FROM STOCK S DEALERS D
WHERE S.Dcode=D.Dcode AND ItemNo=5004;
(iv) SELECT MIN(StockDate) FROM STOCK;

16 | P a g e
ANSWER
Short Answer Questions
1. (i) Candidate Key :It refers to any column/attribute that can uniquely identify record in a
table.
(ii) Primary key : It referes to designated attribute(s)/column(s) that uniquely identifies a
row/tuple in a table/relation. It is one of the candidates keys.
(iii) Foreign key :is an attribute in a table which is the primary key in linked table

2. (i) Degree of the table TRANSACTIONS=4


Cardinality of table CUSTOMERS=3
(ii) TNO PRIMARY KEY; TNO, CNO CANDIDATES KEYS

3 No, Count(*) and Count(<column-name>) are not the same.


While count(*) counts and return the number of records in a table, count(<column-name>)
counts number of records where the mentioned column-name is not null.

4 The problem with the given SQL query is that there is no GROUP BY clause is given and
thus, it will lead to an error.
The reason being that the select list use COUNT() function, which is an aggregate function,
along with a field. When we use an aggregate function in the select list along with a database
field, we need to add a GROUP BY clause.
To correct the error it should use GROUP BY clasue.
SELECT house, count(*) FROM student
GROUP BY house;

7. The Count(*) function returns the total number of records in the table while count(<field>)
will return the count of non-null values in the given field and this is the reason for the different
results by the given queries above.
The field commission must be containing a NULL value and thus count(commission) returned
the count of non-null values and count(*) return total number of records (Irrespective of
NULL values in the field).

8 SELECT Deptcode,Max(Salary)
FROM Employee
GROUP BY Deptcode;

9 (a) SELECT Type, avg(Price) FROM Vehicle GROUP BY Type having Qty>20;
(b) SELECT Company, count(Distinct Type) FROM Vehicle GROUP BY Compnay;

17 | P a g e
(c) SELECT Type, Sum(Price*Qty) FROM Vehicle GROUP BY Type;

10. SELECT Stream, MAX(Agg)


FROM Employee
GROUP BY Stream;

11 (i) SELECT * FROM PARTICIPANTS WHERE CLASS IN(10,12);


OR
SELECT * FROM PARTICIPANTS WHERE CLASS = 10 OR CLASS=12;
(ii) SELECT SNAME, CLASS FROM PARTICIPANTS ORDER BY SNAME;
(iii) SELECT COUNT(*), CLASS FROM PARTICIPANTS GROUP BY CLASS;

12 (i) COUNT(DISTINCT CITITES)

5
(ii) MAX(PUR_DATE)

2019-05-09
(iii) CNAME QTY PUR_DATE

SANYAM 10 2018-11-10
RAHUL 10 2018-10-12
MEHER 20 2019-05-09

13 (i) SELECT * FROM CUSTOMER WHERE CITITES NOT IN(‘DELHI’,


‘MUMBAI’);
(ii) SELECT CNAME, CITITES FROM CUSTOMERS ORDER BY CNAME;
(iii) SELECT COUNT(*), CITIES FROM CUSTOMERS GROUP BY CITIES;
(iv) SELECT * FROM PURCHASES WHERE QTY>15;

14 (i) COUNT(DISTINCT ParticipantsNum)

3
(ii) MAX(ScheduleDate) MIN(ScheduleDate)

19-Mar-04 12-Dec-03
(iii) Name ActivityName

Ravubder Discuss Throw


(iv) DISTINCT ParticipantsNum

16
10
12

18 | P a g e
15. (i) COUNT(DISTINCT Dcode)

3
(ii) QTY*UnitPrice

4400
(iii) Item Dname (iv) MIN(StockDate)

Eraser Big Clear Deals 01-Jan-09

CASE STUDY BASED QUESTIONS


1. A library uses a database management system (DBMS) to store the details of the books that
it stocks, it registered members and the book-loans that the library has made. These details
are stored in a database using the following three relations.
Name of the Database : KV Library
 Book (BookID : Char(5), Title : Varchar(25), Author :Varchar(25), Publisher :
Varchar(100))
 Member(MemberID:Char(5), LastName:Varchar(25), FirstName:Varchar(25),
Correspondence-Address : Varchar(100), Pincode : Char(6), DateofBirth : Date,
EmailID : Varchar(50))
 Loan(MemberID: Char(5), BookID:Char(5), LastDate:Date, DueBackDate:Date,
Returned :Boolean)
Note : The Library does not stock more than one copy of the same book.

(a) Identify the table that uses a composite primary key from the library database. 1
(i) Book Table (ii) Member Table (iii) Loan Table (iv) all of these
(b) I. Identify the possible alternate keys from relations Book and Member. 1
(i) Book : Title (ii) Books Author (iii) Member:EmailID (iv) Member:FirstName’
II. Can the Loan relation have an alternate key ?Why ? 1

(c) Relations Book and Member have the following records : 1


Book
BookID Title Author Publisher

B1103 - - -
B2902 - - -
B2950 - - -
B3100 - - -
B3275 - - -

Member
BookID

B1103
B2902
B2950
B3100
B3275

19 | P a g e
Write an example of the valid record for the loan relation. Write a query to insert a
valid record in the Loan relation.

(d) Write a SQL query to retrieve the names and email addresses of the members who 1
have not returned their books.

2 A library uses database management system(DBMS) to store the details of the books that it
stocks, its registered membes and the book-loan that the library has made. These details are
stored in a database using the following three relations. Name of the Database : KV Library
 Book (BookID : Char(5), Title : Varchar(25), Author :Varchar(25), Publisher :
Varchar(100))
 Member(MemberID:Char(5), LastName:Varchar(25), FirstName:Varchar(25),
Correspondence-Address : Varchar(100), Pincode : Char(6), DateofBirth : Date,
EmailID : Varchar(50))
 Loan(MemberID: Char(5), BookID:Char(5), LastDate:Date, DueBackDate:Date,
Returned :Boolean)
Note : The Library does not stock more than one copy of the same book.

(a) Identify following types of keys from all the relations of the given database Foreign 2
keys along with parent relations.
(b) Can a relation have multiple foreign keys? Give example. 1
(c) Can a foreign key be part of a primary key? Give example. 1
(d)Write a SQL query to retrieve the names and email addresses of the membersbelonging 1
to KVS (they have email ids as @kvs.in) and wo have not returned their
books.

3. FurnFly is a furniture company selling furniture to customers of its store and operates a
follows:
 The store does not keep the furniture in stock.
 The company places orders for the furniture required from its suppliers ONLY AFTER
a customer places an order at the store.
 When the ordered furniture arrives at the store, the customer is informed via
telephone or e-mail that it is ready for delivery.
 Customers often order more than one type of furniture on the same order, for example,
a sofa, two puffy chairs and centre table.
Details of the furniture, customers and orders are to be stored in a relational database using
the following four relations :
Database Name :FurnFly Furnishers
Furniture (FurnitureID : Char(7), FurnitureName : Varchar(50), Category : Varchar(25),
Price : Float, SupplierName : Varchar(100))
CustomerOrder(OrderId : Number(8,0), CustomerID : Char(10), OrderDate:Date)
CustomerOrderLine :(OrderID : Number(8,0), FurnitureID: Char(7), Quantity:
Number(4,0))
Customer :(CustomerID : Char(10), CustomerName:Varchar(100), EmailAddress :
Varchar(30), TelephoneNumber: Number(15,0))

(a) Identify the relationships among tables. 1


(b) Identify the relation having composite primary key and its primary key. 1
(c) Write a SQL query to create table customerOrder. It should also define required 1
primary key and foreign key(s)
20 | P a g e
(d) A fault has been identified with the furnitureID number 6281. The manager needs to 1
known how many orders need to be recalled. Write a SQL query for the same.
(e) A customer with ID number ‘C5104’ wants to change his registered telephone number 1
as 9988776655. Write a SQL query to achieve this.

4. Rachana Mittal runs a beauty parlor. She uses a database management system(DBMS) to
store the information that she needs to manage her business. This information includes
customer contact details, staff names, the treatments that the parlor offer (for example, ‘’Hair
Massage’) and appointment that customers have made for treatments. A separate
appointment must be made for each treatment.
The details are stored in a database using the following four relations:
Customer: (CustomerID, FirstName, LastName, TelephoneNumber, EmailAddress)
Staff :(StaffID, FirstName,LastName, IsQualified)
Treatment: (TreatmentName,Price,TimeTaken,NeedsQualification)
Appointment : (CustomerID,TreatmentName,ApDate,ApTime)
 The IsQualifiedattribute for a member of staff stores one of the value True or False,
to indicate if the member of staff is fully qualified or not.
 The NeedsQualifictionattribute for a treatment stores True or False to indicate if the
treatment can only be given by a qualified member of staff.
 The TimeTakenattribute for a treatment is the number of minutes (a whole number)
that the treatment takes.
(a) Write a SQL statement to create the table staff. 1
(b) Write a query to Insert a record in the table Staff with following data ;
(2009, ‘Sheril’, ‘Mark’, ‘True’)

(c) Which table’s records can be deleted without affecting any other table? 1
(i) Customer (ii) Staff (iii) Treatment (iv) Appointment
(d) Write a query to Modify table Appointment to add a new column StaffID, which
should hold a legal StaffID value from the staff table.
(e) Rachana wants to send e-mail advertisement to all the customers who had a ‘RF 1
Facial’ treatmentin 2020. To send the email, the customer’s email address, firstname
and lastname are needed.
Write a SQL query to retrieve the email address, firstname and lastname of each
customer to whom email should be sent.

5. Consider the table STUDENT given below:

RollNo Name Class DOB Gender City Marks


1 Anand XI 6/6/97 M Agra 430
2 Chetan XII 7/5/94 M Mumbai 460
3 Geet XI 6/5/97 F Agra 470
4 Preeti XII 8/8/95 F Mumbai 492
5 Saniyal XII 8/10/95 M Delhi 360
6 Maakhiy XI 12/12/94 F Dubai 256
7 Neha X 8/12/95 F Moscow 324
8 Nishant X 12/6/95 M Moscow 429

(a) State the command that will give output as:


21 | P a g e
Name

Anand

Chetan

Geet

Preeti

(i) Select Name from student where class= ‘XI’ and class=‘XII’;
(ii) Select Name from student where not class= ‘XI’ and class=‘XII’;
(iii) Select Name from student where city = ‘Agra’ or city = ‘Mumbai’;
(iv) Select Name from student where city IN(‘Agra’ , ‘Mumbai’);

Choose the correct option : 1


(a) Both (i) and (ii)
(b) Both (iii) and (iv)
(c) any of the option (i) , (ii) and (iv)
(d) Only (iii)
(b) What will be the output of the following command?
Select * from student where gender= ‘F’ order by marks;
(i)

RollNo Name Class DOB Gender City Marks


4 Preeti XII 8/8/95 F Mumbai 492
3 Geet XI 6/5/97 F Agra 470
7 Neha X 8/12/95 F Moscow 324
6 Maakhiy XI 12/12/94 F Dubai 256

(ii)
RollNo Name Class DOB Gender City Marks
6 Maakhiy XI 12/12/94 F Dubai 256
7 Neha X 8/12/95 F Moscow 324
3 Geet XI 6/5/97 F Agra 470
4 Preeti XII 8/8/95 F Mumbai 492

(iii)
Gender Marks
F 256
F 324
F 470
F 492

(iv)

22 | P a g e
Gender Marks
F 492
F 470
F 324
F 256

(iii) Prachi has given the following command to obtain the highest marks 1
SELECT max(Marks) from student where group by class;
But she is not getting the desired result. Help her by writing the correct command.
(a) Select max(Marks) from student where group by class;
(b) Select class, max(Marks) from student group by Marks;
(c) Select class, max(Marks) group by class from students;
(d) Select class, max(Marks) from student group by class;
(iv) State the command to display the average marks scored by students of each gender who 1
are in class XI?
(a) Select Gender, avg(Marks) from student where class= ‘XI’ group by gender;
(b) Select Gender, avg(Marks) from student group by gender where class= ‘XI’;
(c) Select Gender, avg(Marks) group by Gender from student having class= ‘XI’;
(d) Select Gender, avg(Marks) from student group by Gender having class= ‘XI’;
Choose the correct option:
(a) Both (ii) and (iii)
(b) Both (ii) and (iv)
(c) Both (i) and (iii)
(d) Only (iii)
(v) Help Ritesh to write the command to display the name of the youngest student.
(a) Select Name, min(DOB) from student;
(b) Select Name, max(DOB) from student;
(c) Select Name, min(DOB) from student group by Name;
(d) Select Name, maximum(DOB) from student;

ANSWER
CASE STUDY BASED QUESTIONS
1. (a) (iii) Loan Table
(b) I. (i) Book : Title (ii) Member: EmailID
II. No, the Loan relation cannot have alternate key as its primary key is a composite key
having foreign key.

(c) INSERT INTO Loan Values(‘M1255’, ‘B3100’, ‘02/02/2020’, ‘09/02/2020’, False)

(d) Select FirstName, LastName, EmailID


From Member, Loan
Where Member.MemberID=Loan.MemberID
AND Returned = ‘False’;

2 (a) Foreign Keys in Relation Loan


MemberID(Parent Table Member)
BookID (Parent Table Book)

23 | P a g e
(b) Yes, a relation can have multiple foreign keys, e.g., the loan relation given above has
two foreign keys – MemberID and BookID

(c) Yes, a foreign key can be a part of composite primary key, e.g., the primary key of
relation loan is : (MemberID, BookID, LoanDate), which contains two foreign keys :
MemberID and BookID.
(d) Select FristName,LastName, EmailID
From Member, Loan
Where Member.MemberID=Loan.MemberID
AND EmailID LIKE “%@kvs.in” AND Returned = ‘False’;

3. (a) Table Related to table (Key)

CustomerOrderCustomer(CustomerID)
CustomerOrderLineCustomerOrder(OrderID)
CustomerOrderLine Furniture (FurnitureID)

(b) CustomerOrderLine(OrderID, FurnitureID)

(c) Create Table CustomerOrder


(OrderIDNumber(8,0) Not Null Primary Key,
CustomerIDchar(ID) REFERENCE Customer(CustomerID),
OrderDate Date);

(d) Select count(*)


From CustomerOrderLine
Group by FurnitureID
Having FurnitureID = ‘6281’;

(e) Update Customer


Set TelephoneNumber=9988776655
Where CustomerID= ‘C5104’;

4. (a) Create Table Staff


( StaffID Number(4,0) NOT NULL PRIMARY KEY,
FirstName Varchar(20) NOT NULL,
LastNameVarchar(20),
ISQualifiedChar(4) Check (IsQualified IN(‘True’, ‘False’)));
(b) INSERT INTO Staff Values(2009, ‘Sheril’, ‘Mark’, ‘True’);

(c) (ii) Staff table’s records can be deleted without affecting any other table as of now,
because this table is not linked with any other table yet.

(d) Alter Table Appointment Add StaffIDNumber(4,0) NOT NULL Reference Staff(StaffID);

24 | P a g e
(e) Select EmailAddress, FirstName,LastName
From Customer C, Appointment A
Where C.CustomerID=A.CustomerID
AND TreatmentName= ‘RF Facial’;

5. (i) (b) Both (iii) and (iv)

(ii) (b)

(iii) (d)

(iv) (b) Both (ii) and (iv)

(v) (b)

25 | P a g e
26 | P a g e

You might also like