Term – 2
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.
92 | P a g e
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