Sqlboard Based Practice Questions for
Sqlboard Based Practice Questions for
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.
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
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
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
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
16. Which of the following SQL commands retrives data from table(s) ? 1
(a) UPDATE (b) SELECT (c) Union (d) All of these
(a) Misha, Khushi (b) Khushi, Japneet (c) Japneet (d)Misha, Japneet
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
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
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
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
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)
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
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
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
8. The null values in a column can be searched for in a table using in the WHERE 1
clause of SELECT query.
10. Columns can be sorted in descending sequence by using the SQL keyword . 1
24. A column added via ALTER TABLE command initially contains value for all rows. 1
30. The clause is used in SELECT queries to specify filtering condition for groups. 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
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
43. To remove a character from the right side of a string, 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
9. join produces a data set that includes only those rows from the left table which have 1
matching rows from the right table.
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
3. How many primary key and unique keys can be there in a table? 1
6. What is a tuple 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
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.
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
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.
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
19. COUNT(*)
12 | P a g e
2. Observe the following tables TRANSACTIONS and CUSTOMERS carefully and answer the 2
questions that follows :
Table : Transaction
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
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.
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
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
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
Table :DEALERS
Dcode Dname
101 Reliable Stationers
103 Classis Plastics
102 Clear Deals
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
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;
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
3
(ii) MAX(ScheduleDate) MIN(ScheduleDate)
19-Mar-04 12-Dec-03
(iii) Name ActivityName
16
10
12
18 | P a g e
15. (i) COUNT(DISTINCT Dcode)
3
(ii) QTY*UnitPrice
4400
(iii) Item Dname (iv) MIN(StockDate)
(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
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))
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.
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’);
(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.
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’;
CustomerOrderCustomer(CustomerID)
CustomerOrderLineCustomerOrder(OrderID)
CustomerOrderLine Furniture (FurnitureID)
(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’;
(ii) (b)
(iii) (d)
(v) (b)
25 | P a g e
26 | P a g e