Objective Type Questions Multiple Choice Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

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

93 | P a g e
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
94 | P a g e
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

95 | P a g e
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.

96 | P a g e
(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

97 | P a g e
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

98 | P a g e
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

99 | P a g e
47. _____ join select all data starting from the left table and matching rows in the right table. 1

48. _____ join is a reversed version of left join. 1

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

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

True and False Questions


1. A primary key can store empty values in it. 1

2. Common attribute of two tables is called a foreign key. 1

3. A common attribute of two tables is called a foreign key it is the primary key in one table and 1
the other table reference it.

4. Part of SQL which creates and defines tables and other database objects, is called DDL 1

5. Part of SQL which manipulates data in tables, is called TCL 1

6. Part of SQL which access and manipulates data in tables is called DML 1

7. Part of SQL which controls transactions, is called TCL. 1

8. MySQL is name of customized query language used by Oracle. 1

9. SQL is a case sensitive.

10. The condition in a WHERE clause in a SELECT query can refer to only one value. 1

11. SQL provides the AS keyword, which can be used to assign meaningful column name to the 1
results of queries using the SQL built-in functions.

12. SQL is a programing language. 1

13. SELECT DISTINCT is used if a user wishes to see duplicate columns in a query. 1

14. ORDER BY can be combined with SELECT statement. 1

15. DELETE FROM <table> command is same as DROM TABLE <table> command. 1

16. The unique constraint can only be defined once in the CREATE TABLE command. 1

17. Unique and Primary Key constraints are the same. 1

18. Tuple based constraints can use multiple columns of the table. 1

100 | P a g e
19. The table based constraints can use multiple column of the table. 1

20. You can add a column with a NOT NULL constraint using ALTER TABLE, only to a table 1
that contains no rows.

21. You can use the INSERT statement only to add one new row and not multiple new rows to 1
an existing table.

22. The HAVING and WHERE clause are interchangeable. 1

23. The HAVING clauses can take any valid SQL function in its condition. 1

24. Truncate() is a text function. 1

25. Length() is a numeric function. 1

26. Functions MID() and SUBSTR() do the same thing. 1

27. INSTR() and SUBSTR() work identically. 1

28. Natural Join contains the duplicate columns 1

29. Equi Join contains the duplicate columns 1

30. Non- Equi Join is the name of Natural Join 1

101 | P a g e
Very Short Answer 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

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

20. Write a query to create a string from the ASCII values 70,65,67,69 1

21. Write a query to concatenate the two strings : “Hello” and “World” 1

22. Display 4 characters extracted from 5th right character onwards from the string ‘ABCDEFG’ 1

23. Convert and display string ‘Large’ into uppercase 1


102 | P a g e
24. Write a query to remove leading space of the string ‘ RDBMS MySQL’. 1

25. Display the position of occurrence of string ‘OR’ in the string ‘CORPORATE FLOOR’ 1

26. How many characters are there in the string ‘CANDIDE’ 1

27. Write a query to extract 2 digit year from a string ‘USS/23/67/09’. The last two character tell 1
the year.

28. Write a query to extract institute code from a string ‘USS/23/67/09’. The first three characters 1
tell the institute code.

29. Write a query to find out the remainder of 11 divide by 4. 1

30. Write a query to round off value 15.193 to one decimal place. 1

31. Write a query to find out the square root of value 26. 1

32. Write a query to truncate value 15.79 to 1 decimal place. 1

33. Write a query to display current date on your system. 1

34. Write a query to extract month part from date 3rd Feb 2021 1

35. Write a query to display name of weekday for date 03rd Feb 2021 1

ANSWER
Multiple Choice Questions
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

103 | P a g e
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
True / False
1 False 2 False 3 True 4 True
5 False 6 True 7 True 8 False
9 False 10 False 11 True 12 False
13 False 14 True 15 False 16 False
17 False 18 False 19 True 20 True
21 False 22 False 23 False 24 False
25 False 26 True 27 False 28 False
29 True 30 False

Very Short Answer Questions


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.

104 | 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(*)

20. SELECT CHAR(70,65,67,69); 1


-----------------------------------
FACE

21 SELECT CONCAT(‘Hello’, ‘World’); 1


-----------------------------------------
HelloWorld

22 SELECT SUBSTR(‘ÁBCDEFG’,-5,4) ‘SUBS’; 1


-----------------------------------------------------
CDEF

23 SELECT UPPER(‘Large’) ‘uppercase’; 1


----------------------------------------------- LARGE

24 SELECT LTRIM(‘ RDBMS MySQL’); 1


105 | P a g e
-------------------------------------------------
RDBMS MySQL

25 SELECT INSTR(‘CORPORATE FLOOR’, ‘OR’); 1


----------------------------------------------------------
2

26 SELECT LENGTH(‘CANDIDE’) 1
---------------------------------------
7

27 SELECT RIGTH(‘ÚSS/23/67/09’,2); 1
-------------------------------------
09

28 SELECT LEFT(‘ÚSS/23/67/09’,3); 1
-------------------------------------
USS

29. SELECT MOD(11,4); 1


------------------------
3

30 SELECT ROUND(15.193,1); 1
---------------------------
15.2

31 SELECT SQRT(26); 1
----------------------
5.09901951

32 SELECT TRUNCATE(15.79,1); 1
------------------------------------
15.7

33. SELECT CURDATE(); 1

34 SELECT MONTH(‘2021-02-03’); 1
---------------------------------------
02

35 SELECT DAYNAME(‘2021-02-03’); 1
---------------------------------------
Wednesday

106 | P a g e
SHORT ANSWER QUESTIONS
1. Explain each of the following with illustrations using a table 3
(i) Candidate Key (ii) Primary Key (iii) Foreign Key

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;

5. Consider the following SQL string : ‘Preoccupied’. Write commands to display: 2


(a) ‘occupied’ (b) ‘cup’

6. Consider the same string : ‘Preoccupied’. Write commands to display: 2


(a) The position of the substring ‘cup’ in the string ‘Preoccupied’.
(b) The first 4 letters of the string.

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.

107 | 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

108 | 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

109 | 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;

110 | 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;

5. (a) SELECT substr(‘’Preoccupied’, 4); OR SELECT substring(‘Preoccupied’,4);


Or
SELECT mid(‘’Preoccupied’, 4);

(b) SELECT substr(‘’Preoccupied’, 6,3); OR SELECT substring(‘Preoccupied’,6,3);


Or
SELECT mid(‘’Preoccupied’, 6,3);

6 (a) SELECT instr(‘Preoccupied’, ‘cup’);


(b) SELECT left(‘Preoccupied’, 4);

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;

111 | 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

112 | 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

113 | 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 members 1
belonging 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)
114 | 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:


115 | 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)

116 | 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)

117 | 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);

118 | 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)

119 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy