SQL Revision Tourr Question Bank
SQL Revision Tourr Question Bank
SQL Revision Tourr Question Bank
Answer
SQL (Structured Query Language) is a standard language for accessing and manipulating databases.
Question 2
Answer
Data types are means to identify the type of data and associated operations for handling it. The data
types available in MySQL are int, float, date, time, char, varchar etc.
Question 3
Answer
To calculate 13 * 15 in SQL, we can use SELECT statement to retrieve rows computed without reference
to any table. For example,
Question 4
Is NULL value the same as 0 (zero) ? Write the reason for your answer.
Answer
No, a NULL value is not the same as 0 (zero) in SQL. In SQL, NULL represents a missing or unknown value
and is a legal empty value. On the other hand, 0 is a specific numeric value representing the number
zero.
Question 5
Write the UPDATE command to increase the commission (Column name : COMM) by 500 of all the
Salesmen who have achieved Sales (Column name : SALES) more than 200000. The table's name is
COMPANY.
Answer
UPDATE COMPANY
Question 6
While using SQL pattern matching, what is the difference between '_' (underscore) and '%' wildcard
symbols ?
Answer
In SQL pattern matching, the underscore ('_') character matches any character, while the percent ('%')
wildcard character matches any substring.
Question 7
Write one similarity and one difference between CHAR and VARCHAR data types.
Answer
One similarity between the CHAR and VARCHAR data types in SQL is that both are used to store
character data.
One difference between the CHAR and VARCHAR data types in SQL is that the Char datatype specifies a
fixed length character string, while the Varchar datatype specifies a variable length string.
Question 8
Answer
Question 9
Write a command to add a NOT NULL constraint on FEES column of a student table.
Answer
Question 10
What is a constraint ? Name some constraints that you can apply to enhance database integrity.
Answer
A constraint is a condition or check applicable on a field or set of fields. Some constraints that we can
apply to enhance database integrity are:
1. Unique constraint.
3. Default constraint.
4. Check constraint.
Question 11
Answer
A primary key is a unique identifier for each record in a table, and it must be unique and not null. A
PRIMARY KEY constraint declares a column or one group of columns as the primary key of the table. This
constraint must be applied to columns declared as NOT NULL.
Question 12
Answer
The NOT NULL constraint is used in SQL to ensure that a column cannot contain NULL (i.e., empty)
values.
A DEFAULT constraint is used in SQL to specify a default value for a column in a table. When the user
does not enter a value for the column (having default value), automatically the defined default value is
inserted in the field.
Question 13
When a column's value is skipped in an INSERT command, which value is inserted in the database ?
Answer
The columns that are not listed in the INSERT command will have their default value, if it is defined for
them, otherwise, NULL value. If any other column (that does not have a default value and is defined NOT
NULL) is skipped or omitted, an error message is generated and the row is not added.
Question 14
Answer
SHOW DATABASES;
Question 15
Write MySql command will be used to open an already existing database "CONTACTS".
Answer
USE CONTACTS;
Question 1
4. None of these
Answer
Question 2
Answer
Question 3
Answer
Data Manipulation language.
Question 4
Which is the subset of SQL commands used to manipulate database structures, including tables ?
4. None
Answer
Reason — Data Definition Language (DDL) commands are used to define and manipulate database
structures, including creating, altering, and dropping tables, indexes, views, and other schema objects.
Question 5
Which of the following sublanguages of SQL is used to define the structure of the relation, deleting
relations and relating schemas ?
3. Query
4. Relational Schema
Answer
Reason — In SQL, Data Definition Language (DDL) statements are used to define the structure of the
database, including creating, relating, altering, and dropping database objects such as tables, indexes,
and views.
Question 6
Which of the following sublanguages of SQL is used to query information from the database and to insert
tuples into, delete tuples from, and modify tuples in the database ?
3. Query
4. Relational Schema
Answer
Reason — In SQL, Data Manipulation Language (DML) statements are used to manipulate data in the
database. DML statements are used to query information from the database, as well as to insert, delete,
and modify tuples (rows) in the database tables.
Question 7
1. Date
2. Integer
3. Year
4. Month
Answer
Year, Month
Reason — In SQL, 'Year' and 'Month' are not valid data types.
Question 8
1. SELECT
2. ALTER
3. INSERT
4. UPDATE
Answer
ALTER
Reason — DDL (Data Definition Language) commands in SQL are used to create and define tables and
other database objects. Examples of DDL commands include ALTER, which is used to modify objects like
tables, indexes, views, and constraints. On the other hand, SELECT, INSERT, and UPDATE commands are
part of DML (Data Manipulation Language), used for retrieving, inserting, and updating data within the
database.
Question 9
Which of the following keywords will you use in the following query to display the unique values of the
column dept_name?
SELECT ............... dept_name FROM Company;
1. All
2. From
3. Distinct
4. Name
Answer
Distinct
Reason — The DISTINCT keyword is used to display the unique values of the column.
Question 10
Which of the following keywords will you use in the following query to display all the values of the
column dept_name ?
1. All
2. From
3. Distinct
4. Name
Answer
All
Reason — The All keyword is used to display all values of the column.
Question 11
The ............... clause of SELECT query allows us to select only those rows in the result that satisfy a
specified condition.
1. Where
2. from
3. having
4. like
Answer
Where
Reason — The WHERE clause in SELECT statement specifies the criteria for selection of rows to be
returned. When a WHERE clause is present, the database program goes through the entire table one row
at a time and examines each row to determine if the given condition is true. If it is true for a row, that
row is displayed in the output.
Question 12
Which comparison operator may be used to fill the blank space in above query ?
1. =
2. LIKE
3. IS
4. IS NOT
Answer
IS, IS NOT
Reason — In SQL, to check for NULL values, we must use the IS NULL or IS NOT NULL comparison
operators. The "=" operator cannot be used for NULL because NULL represents an unknown value, and
comparisons using = (equal to) will not work as expected. The LIKE operator is used for pattern matching
with strings.
Question 13
In SQL, which command is used to SELECT only one copy of each set of duplicable rows ?
1. SELECT DISTINCT
2. SELECT UNIQUE
3. SELECT DIFFERENT
Answer
SELECT DISTINCT
Reason — The SELECT DISTINCT statement is used in SQL to return only unique (distinct) values within
the result set. It removes duplicate records, ensuring that each row in the result set is unique.
Question 14
Which of the following types of table constraints will prevent the entry of duplicate rows ?
1. Unique
2. Distinct
3. Primary Key
4. NULL
Answer
Primary Key
Reason — A primary key is a set of one or more attributes or fields that uniquely identifies a tuple or row
in a table. Therefore, it ensures that each row in the table is unique and prevents the entry of duplicate
rows.
Question 15
Ravisha has stored the records of all students of her class in a MYSQL table. Suggest a suitable SQL clause
that she should use to display the names of students in alphabetical order.
1. SORT BY
2. ALIGN BY
3. GROUP BY
4. ORDER BY
Answer
ORDER BY
Reason — The suitable SQL clause that Ravisha should use to display the names of students in
alphabetical order is ORDER BY. The ORDER BY clause in SQL is used to sort the result set of a query, and
by default, it sorts the results in ascending order.
Question 1
Question 2
The SQL keyword FROM is used to specify the table(s) that contains the data to be retrieved.
Question 3
To remove duplicate rows from the result of a query, specify the SQL qualifier DISTINCT in select list.
Question 4
To obtain all columns, use a(n) asterisk(*) instead of listing all the column names in the select list.
Question 5
The SQL WHERE clause contains the condition that specifies which rows are to be selected.
Question 6
When two conditions must both be true for the rows to be selected, the conditions are separated by the
SQL keyword AND.
Question 7
To refer to a set of values needed for a condition, we can use the SQL operator IN.
Question 8
To exclude one or more values (a list of values) using a condition, the SQL keyword NOT IN should be
used.
Question 9
The SQL keyword LIKE is used in SQL expressions to select based on patterns.
Question 10
The UPDATE command can be used to make changes in the rows of a table in SQL.
True/False Questions
Question 1
The condition in a WHERE clause in a SELECT query can refer to only one value.
Answer
False
Reason — In SQL, the condition in a WHERE clause can refer to multiple values. We can use logical
operators such as AND, OR, and NOT to combine multiple conditions. For example :
SELECT * FROM pet WHERE (species = 'cat' OR species = 'dog') AND sex = 'm';
Question 2
SQL provides the AS keyword, which can be used to assign meaningful column names to the results of
queries using the SQL built-in functions.
Answer
True
Reason — SQL provides the AS keyword, which can be used to assign meaningful column names to the
results of queries using the SQL built-in functions. The syntax is as follows :
SELECT <column name> AS [column alias] [, <column name> AS [column alias]] FROM <table name> ;
Question 3
False
Reason — SQL, Structured Query Language, is a non-procedural query language. It describes WHAT all
data is to be retrieved or inserted or modified or deleted, rather than specifying code describing HOW to
perform the entire operation. Hence, it is not a programming language.
Question 4
Answer
False
Reason — SELECT DISTINCT statement is used if a user wishes to eliminate duplicate rows from the
results of a query.
Question 5
Data manipulation language (DML) commands are used to define a database, including creating, altering,
and dropping tables and establishing constraints.
Answer
False
Reason — Data definition language (DDL) commands in SQL are used to define a database, including
creating, altering, and dropping tables and establishing constraints.
Question 6
The keyword LIKE can be used in a WHERE clause to refer to a range of values.
Answer
False
Reason — The keyword BETWEEN in SQL can be used in a WHERE clause to refer to a range of values.
While the keyword LIKE can be used in a WHERE clause for comparison of character strings using
patterns.
Question 7
The keyword BETWEEN can be used in a WHERE clause to refer to a range of values.
Answer
True
Reason — The keyword BETWEEN in SQL can be used in a WHERE clause to refer to a range of values.
Assertion. The UNIQUE and PRIMARY KEY constraints are similar but not the same.
Reason. There can be only one column with PRIMARY KEY constraint, in a table.
Answer
Explanation
Both the UNIQUE and PRIMARY KEY constraints ensure unique values for each row in a column.
However, UNIQUE allows NULL values, whereas PRIMARY KEY does not. There can exist multiple columns
with UNIQUE constraints in a table, but only one column can have a PRIMARY KEY constraint.
Question 2
Assertion. In terms of values allowed in a column, both UNIQUE and PRIMARY KEY constraints are not
the same.
Reason. UNIQUE allows NULL value once in the column, but PRIMARY KEY does not.
Answer
Explanation
The UNIQUE and PRIMARY KEY constraints are similar in that they both ensure uniqueness of values in a
column or set of columns. However, they differ in terms of the values they allow, particularly regarding
NULL values. UNIQUE allows NULL values, whereas PRIMARY KEY does not.
Question 3
Assertion. The INSERT INTO statement can skip some columns' values.
Reason. Only the columns allowing the NULL values or have default-value-defined, can be skipped in
INSERT INTO statement of SQL.
Answer
Explanation
The INSERT INTO statement allows skipping columns values for columns that permit NULL values or have
default values defined. If any other column (that does not have a default value and is defined as NOT
NULL) is skipped or omitted, an error message is generated, and the row is not added.
Question 4
Assertion. The PRIMARY KEY can be defined only once in the CREATE TABLE command.
Reason. If the PRIMARY KEY is a composite key, then it is not defined with the individual columns but at
the end of the table definition as a table constraint.
Answer
Explanation
In SQL, when we define a table using the CREATE TABLE command, we can only specify one PRIMARY KEY
constraint. This PRIMARY KEY can consist of a single column or multiple columns (composite key). When
a PRIMARY KEY is a composite key (consisting of multiple columns), it's not defined with each individual
column but rather as a table constraint at the end of the table definition.
Question 5
Reason. The DML commands manipulate the data stored in the database tables.
Answer
Both A and R are true and R is the correct explanation of A.
Explanation
INSERT, UPDATE, DELETE, and SELECT are the DML (Data Manipulation Language) commands in SQL.
These commands manipulate the data stored in the database tables. INSERT adds new records, UPDATE
modifies existing records, DELETE removes records, and SELECT retrieves data based on specified criteria.
Question 6
Assertion. The PRIMARY KEY and FOREIGN KEY constraints are similar.
Reason. The FOREIGN KEY constraint links a column of a table with the PRIMARY KEY constraint of
another table.
Answer
Explanation
The PRIMARY KEY and FOREIGN KEY constraints are similar in that they both deal with unique identifiers.
However, they serve different purposes, PRIMARY KEY uniquely identifies each record in a table while
FOREIGN KEY establishes a relationship between two tables by linking a column (or set of columns) to
the PRIMARY KEY of another table.
Question 7
Assertion. Data types varchar and char are the same as they both represent the string data.
Reason. The VARCHAR datatype stores variable string length while CHAR datatype stores the string
always as fixed length strings.
Answer
Explanation
The VARCHAR and CHAR data types are not the same, as they differ in how they store string data. The
VARCHAR data type stores variable-length string, while the CHAR data type stores strings as fixed-length
character strings.
Assignment
Question 1
Answer
Question 2
Answer
Examples of DDL commands are CREATE, ALTER, DROP, GRANT, Examples of DML commands are INSERT,
ANALYZE etc. UPDATE, DELETE, SELECT etc.
Question 3
2 Sanchay IP 100
3 Vinesh CS 90
4 Sneha IP 99
5 Akshita IP 100
(i) Insert a new record in the table having following values : [6, 'Khushi', 'CS', 85]
(iii) To remove the records of those students whose marks are less than 30.
Answer
(i)
(ii)
UPDATE exam
(iii)
(iv)
Question 4(a)
What is the use of UPDATE statement in SQL ? How is it different from ALTER statement ?
Answer
The UPDATE statement is used to modify existing records in a table. It specifies the rows to be changed
using the WHERE clause, and sets the new data using the SET keyword. In contrast, the ALTER statement
is used to modify the structure of a table, such as adding, modifying, or deleting columns, and adding or
dropping constraints.
Question 4(b)
Mr. Shankar created a table VEHICLE with 3 rows and 4 columns. He added 1 more row to it and deleted
one column. What is the Cardinality and Degree of the Table VEHICLE ?
Answer
Explanation — Initially, the table VEHICLE created by Mr. Shankar had 3 rows and 4 columns, with a
cardinality of 3 and a degree of 4. After modifying the table by adding 1 row and deleting 1 column, the
new cardinality of the table VEHICLE is 4 and the degree is 3.
Question 4(c)
Consider the following table named "GYM" with details about fitness items being sold in the store. Write
command of SQL for (i) to (iv).
Table : GYM
(i) To display the names of all the items whose name starts with "A".
(ii) To display ICODEs and INAMEs of all items, whose Brandname is Reliable or Coscore.
(iii) To change the Brandname to "Fit Trend India" of the item, whose ICODE as "G101".
(iv) Add a new row for new item in GYM with the details :
Answer
(i)
SELECT INAME
FROM GYM
Output
+-------------------+
| INAME |
+-------------------+
+-------------------+
(ii)
FROM GYM
Output
+-------+----------------------+
| ICODE | INAME |
+-------+----------------------+
(iii)
UPDATE GYM
(iv)
Question 5(a)
Mr. James created a table CLIENT with 2 rows and 4 columns. He added 2 more rows to it and deleted
one column. What is the Cardinality and Degree of the Table CLIENT ?
Answer
Explanation — Initially, the table CLIENT created by Mr. James had 2 rows and 4 columns, with a
cardinality of 2 and a degree of 4. After modifying the table by adding 2 rows and deleting 1 column, the
new cardinality of the table CLIENT is 4 and the degree is 3.
Question 5(b)
Consider the following table FITNESS with details about fitness products being sold in the store. Write
command of SQL for (i) to (iv).
Table : FITNESS
(iii) To change the price data of all the products by applying 25% discount reduction.
Answer
(i)
Output
+-----------+
| PNAME |
+-----------+
| Treadmill |
| Multi Gym |
+-----------+
(ii)
SELECT PNAME
FROM FITNESS
Output
+-------+
| PNAME |
+-------+
| Bike |
+-------+
(iii)
UPDATE FITNESS
(iv)
INSERT INTO FITNESS
Question 6
Write SQL commands for the following on the basis of given table CLUB.
Table : CLUB
(a) To show all information about the swimming coaches in the club.
(b) To list names of all coaches with their date of appointment (DATOFAPP) in descending order.
(c) To display a report, showing coachname, pay, age and bonus (15% of pay) for all the coaches.
Answer
(a)
SELECT *
FROM CLUB
+----------+-----------+-----+----------+-----+-----+------------+
+----------+-----------+-----+----------+-----+-----+------------+
+----------+-----------+-----+----------+-----+-----+------------+
(b)
FROM CLUB
Output
+-----------+------------+
| COACHNAME | DATOFAPP |
+-----------+------------+
| KETAKI | 1998-02-24 |
| ZAREEN | 1998-02-22 |
| ANKITA | 1998-02-20 |
| KARAN | 1998-02-19 |
| SHAILYA | 1998-02-19 |
| RAVINA | 1998-01-20 |
| KUSH | 1998-01-13 |
| ZUBIN | 1998-01-12 |
| TARUN | 1998-01-01 |
| KUKREJA | 1996-03-27 |
+-----------+------------+
(c)
Output
+-----------+------+-----+--------+
+-----------+------+-----+--------+
+-----------+------+-----+--------+
Question 7
Write SQL commands for the following on the basis of given table STUDENT1.
Table : STUDENT1
(b) List the names of those students who are in class 12 sorted by Stipend.
Answer
(a)
SELECT *
FROM STUDENT1
Output
+-----+--------+---------+------------+---------+-------+-------+
+-----+--------+---------+------------+---------+-------+-------+
+-----+--------+---------+------------+---------+-------+-------+
(b)
SELECT Name
FROM STUDENT1
Output
+--------+
| Name |
+--------+
| Divya |
| Mohan |
| Arun |
| Karan |
| John |
| Rubina |
| Vikas |
+--------+
(c)
SELECT *
FROM STUDENT1
Output
+-----+---------+---------+------------+---------+-------+-------+
+-----+---------+---------+------------+---------+-------+-------+
+-----+---------+---------+------------+---------+-------+-------+
Question 8
What is foreign key ? How do you define a foreign key in your table ?
Answer
A non-key attribute, whose values are derived from the primary key of some other table, is known as
foreign key in its current table. Defining a foreign key in a table involves specifying the relationship
between the tables and setting up rules for data integrity. When two tables are related by a common
column or set of columns, the related column(s) in the parent table (or primary table) should be either
declared as a primary key or unique key. Meanwhile, the related column(s) in the child table (or related
table) should have a foreign key constraint referencing the primary or unique key in the parent table.
Question 9
Answer
The PRIMARY KEY is a set of one or more attributes that can uniquely identify tuples within the relation.
A primary key column cannot contain NULL values, and it must have unique values for each row. Only
one primary key constraint can exist per table. Conversely, the FOREIGN KEY command establishes a
relationship between two tables by linking a column or set of columns in one table to the primary key or
a unique key in another table. It enforces referential integrity, ensuring that values in the foreign key
column(s) of the referencing table match values in the referenced table's primary key or unique key
column(s). A foreign key can allow NULL values, indicating that the relationship is optional. Multiple
foreign key constraints can exist in a table, each referencing a different parent table.
Question 10
Answer
FOREIGN KEY commands establish relationships between tables by linking columns in one table to the
PRIMARY KEY or a unique key in another table. This linkage ensures referential integrity, meaning that
values in the FOREIGN KEY column(s) of the referencing table must match values in the PRIMARY KEY or
unique key column(s) of the referenced table. Therefore, FOREIGN KEY commands are directly related to
PRIMARY KEY commands as they rely on the unique identification provided by PRIMARY KEY constraints
in other tables.
Question 11
What are table constraints ? What are column constraints ? How are these two different ?
Answer
Table constraints are rules or conditions applied to an entire table in a database. They are defined when
creating or altering a table's schema.
Column constraints are rules or conditions applied to individual columns within a database table. They
are specified at the column level when creating or altering a table's schema.
The difference between the two is that column constraints apply only to individual columns, whereas
table constraints apply to groups of one or more columns.
Question 12
Insert all those records of table Accounts into table Pending where amt_outstanding is more than 10000.
Answer
Question 13
Answer
Table employee
UPDATE employee
Output
To view all the details (all columns and rows) of the "employee" table the below query is executed :
+----+------------+-----------+----------+--------+
+----+------------+-----------+----------+--------+
Question 14
Add a constraint (NN-Grade) in table Empl (given before assignment) that declares column Grade not
null.
Answer
Question 15
Answer
Question 16(i)
Answer
This command is used to delete a specific table from This command is used to delete an entire database
the database along with all its data, indexes, triggers, including all its tables, views, stored procedures, triggers,
and constraints. and other objects.
The syntax is : DROP TABLE table_name;. The syntax is : DROP DATABASE database_name;.
Question 16(ii)
Answer
This command is used to delete a specific table from the This command is used to remove a specific
database along with all its data, indexes, triggers, and component of a table, such as columns, constraints,
constraints. or indexes.
Question 17
He needs to display names of students who have not been assigned any stream or have been assigned
stream_name that ends with "computers".
He wrote the following command, which did not give the desired result.
Help Mr. Mittal to run the query by removing the error and write correct query.
Answer
The error in Mr. Mittal's original query lies in using "= NULL" instead of "IS NULL" to check for NULL
values and using = '%computers' instead of "LIKE '%computers'" for pattern matching.
SELECT Name
FROM Students
Question 18
Doc_name
Avinash
Hariharan
Vinayak
Deepak
Sanjeev
Answer
Output
+----------+
| doc_name |
+----------+
| Sanjeev |
+----------+
Explanation
The query SELECT doc_name FROM HOSPITAL WHERE Doc_name like "%v"; selects the doc_name from
the HOSPITAL table where the Doc_name column ends with the letter "v" using the LIKE operator with
the "%" wildcard. This pattern matches any string where "v" is the last character, and any characters can
precede it.
Output
+----------+
| doc_name |
+----------+
| Deepak |
| Sanjeev |
+----------+
Explanation
The query SELECT doc_name FROM HOSPITAL WHERE doc_name like "%e%"; selects the doc_name from
the HOSPITAL table where the doc_name column contains the letter "e". This is achieved using
the LIKE operator with the "%" wildcard before and after the letter "e", which matches any sequence of
characters that have "e" in them.
Question 19
Sarthak, a student of class XII, created a table "Class". Grade is one of the columns of this table. To find
the details of students whose Grades have not been entered, he wrote the following MySql query, which
did not give the desired result:
Help Sarthak to run the query by removing the errors from the query and write the correct query.
Answer
The error in Sarthak's code is that he should use the IS NULL comparison instead of = "Null" because the
correct syntax to check for NULL values in SQL is to use the IS NULL operator.
Question 20
What is the purpose of ALTER TABLE command ? Can you add new columns with constraints such as NOT
NULL ? Give example to justify your answer.
Answer
The ALTER TABLE command is used to change definitions of existing tables. It is used to add columns,
integrity constraints and redefine a column (datatype, size, default value) in a table.
Yes, we can add new columns with constraints such as NOT NULL, which ensures that a column must
always contain a value (i.e., cannot be empty or null).
For example, to add a new column tel_number with the NOT NULL constraint in the Empl table, the
statement is:
Question 21
What is the purpose of DROP TABLE command in MySql ? How is it different from DELETE command ?
Answer
The DROP TABLE command in MySql is used to permanently delete an entire table from the database,
including its structure, data, indexes, triggers, and constraints. The DELETE command, on the other hand,
is used to remove specific rows or all rows from a table, leaving the table structure intact.