SQL Interview Questions 2021
SQL Interview Questions 2021
2. What is RDBMS?
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2,
Oracle, My SQL, and Microsoft Access.
The data in an RDBMS is stored in database objects which are called as tables
Relation- table
Tuple- row
Attribute- column
What is table - The data in RDBMS is stored in database objects called tables.
-
What is field Every table is broken up into smaller entities called fields. The fields in the
CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY.
SQL Constraints
Constraints are used to limit the type of data that can go into a table.
Constraints can be specified when a table is created (with the CREATE TABLE statement) or
After he table is created (with the ALTER TABLE statement).
Here are the most important constraints:
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database table.
FOREIGN Key: Uniquely identified a rows/records in any another database table.
CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain
conditions.
INDEX: Use to create and retrieve data from the database very quickly.
4. DEFAULT Constraint:
The DEFAULT constraint provides a default value to a column when the INSERT INTO statement
does not provide a specific value.
A table can have only one primary key, which may consist of single or multiple fields. When
multiple fields are used as a primary key, they are called a composite key.
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
7. FOREIGN Key:
Foreign Key is a column or a combination of columns whose values match a Primary Key in a
different table.
The relationship between 2 tables matches the Primary Key in one of the tables with a
Foreign Key in the second table.
CUSTOMERS table:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
ORDERS table:
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);
8. CHECK Constraint:
The CHECK Constraint enables a condition to check the value being entered into a record. If the
condition evaluates to false, the record violates the constraint and isn’t entered into the table.
Example:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK (AGE >= 18),
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
9. INDEX:
The INDEX is used to create and retrieve data from the database very quickly. Index can be created
by using single or group of columns in a table.
syntax:
To create an INDEX on AGE column, to optimize the search on customers for a particular age, following is the SQL
Example :
Database Normalization
Database normalization is the process of efficiently organizing data in a database. There
are two reasons of this normalization process:
Eliminating redundant data. For example, storing the same data in more than one table.
Ensuring data dependencies make sense.
1NF example:
11. Functional Dependency: The value of one attribute (the determinant) determines the value
of another attribute.
AB reads “Attribute B is functionally dependent on A”
AB means if two rows have same value of A they necessarily have same value of B
FDs are determined by semantics: You can’t say that a FD exists just by looking at data.
But can say whether it does not exist by looking at data.
• Id Name?
• Age Gender?
• Name Id?
• Name, Age Id?
Functional Dependencies and Keys
Functional Dependency: The value of one attribute (thedeterminant) determines the value of
another attribute.
12. Candidate Key :
Attribute that uniquely identifies a row in a relation
Could be a combination of (non-redundant) attributes
Each non-key field is functionally dependent on every candidate key
3NF Example
• ClassroomCapacity TRANSITIVE
• Any partial FDs? NO
• Any transitive FDs? YES !
• How do we eliminate it?
• By breaking into its own table
3NF Normalization
BCNF Example
Assume that
For each subject, each student is taught by one Instructor
Each Instructor teaches only one subject
Each subject is taught by several Instructors
Course, Student Instructor
Course Instructor Student Instructor Course
CS 121 Dr. A. James Bill Payne
CS 121 Dr. A. James Tony Perez
CS 121 Dr. A. James James Atkinson
CS 121 Dr. A. James Linda Lee
17. 4NF
A multi-valued dependency exists when
There are at least 3 attributes A, B, C in a relation and
For each value of A there is a well defined set of values for B, and a well defined set of
values for C,
But the set of values for B is independent on the set of values for C
4NF Example
Assume that
Each subject is taught by many Instructors
The same books are used in many subjects
Each Instructor uses a different book
SQL Fundamentals
Data Definition Language (DDL)
18. CREATE TABLE :
CREATE creates an object (a table, for example) in the database.
Syntax :
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
Example :
SQL> CREATE TABLE SALARY AS SELECT ID, SALARY FROM CUSTOMERS;
19.DROP
deletes an object in the database, usually irretrievably. (Deletes entire table)
Syntax:
DROP TABLE table_name;
Example:
SQL> DROP TABLE CUSTOMERS;
20.ALTER TABLE
ALTER TABLE command is used to add, delete or modify columns in an existing table.
Syntax:
The basic syntax of ALTER TABLE to add a new column in an existing table is as follows:
ALTER TABLE table_name ADD column_name datatype;
The basic syntax of ALTER TABLE to DROP COLUMN in an existing table is as follows:
ALTER TABLE table_name DROP COLUMN column_name;
The basic syntax of ALTER TABLE to change the DATA TYPE of a column in a table is as
follows:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
The basic syntax of ALTER TABLE to add a NOT NULL constraint to a column in a table is as
follow
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;
The basic syntax of ALTER TABLE to ADD UNIQUE CONSTRAINT to a table is as follows:
ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);
The basic syntax of ALTER TABLE to ADD CHECK CONSTRAINT to a table is as follows:
ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);
The basic syntax of ALTER TABLE to ADD PRIMARY KEY constraint to a table is as follows:
ALTER TABLE table_name
ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);
Example:
Consider the CUSTOMERS table having the following records
21.TRUNCATE TABLE
TRUNCATE TABLE command is used to delete complete data from an existing table.
Syntax: TRUNCATE TABLE table_name;
22.INSERT
INSERT INTO Statement is used to add new rows of data to a table in the database.
Syntax:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)] VALUES (value1,
value2, value3,...valueN);
Example:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
23.SELECT
SELECT Statement is used to fetch the data from a database table which returns data in the
form of Result table.
Syntax:
SELECT column1, column2, columnN FROM table_name;
SELECT * FROM table_name;
Example:
24. UPDATE
UPDATE Query is used to modify the existing records in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Example:
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
25. DELETE
DELETE Query is used to delete the existing records from a table.
Syntax:
DELETE FROM table_name
Example:
DELETE FROM table_name
Transaction Control:
There are following commands used to control transactions:
COMMIT: to save the changes.
ROLLBACK: to rollback the changes.
SAVEPOINT: creates points within groups of transactions in which to ROLLBACK
Syntax:COMMIT;
Example:
SQL> DELETE FROM CUSTOMERS
WHERE AGE = 25;
SQL> COMMIT;
Syntax : ROLLBACK;
Example:
SQL> DELETE FROM CUSTOMERS
WHERE AGE = 25;
SQL> ROLLBACK;
30.SAVEPOINT :
A SAVEPOINT is a point in a transaction when you can roll the transaction back to a certain
point without rolling back the entire transaction.
Syntax :
SAVEPOINT SAVEPOINT_NAME;
Example :
SQL> SAVEPOINT SP1;
Savepoint created.
SQL> DELETE FROM CUSTOMERS WHERE ID=1;1 row deleted.
31. INNER JOIN: The INNER JOIN creates a new result table by combining column values of
two tables (table1 and table2) based upon the join-predicate
Syntax:
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_filed = table2.common_field;
Example:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Example:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
33.RIGHT JOIN:
RIGHT JOIN returns all rows from the right table, even if there are no matches in the left
table
Syntax:
SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.common_filed = table2.common_field;
Example:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
34.FULL JOIN
The SQL FULL JOIN combines the results of both left and right outer joins.
Syntax:
SELECT table1.column1, table2.column2...
FROM table1
FULL JOIN table2
ON table1.common_filed = table2.common_field;
Example:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
FULL JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
35.SELF JOIN
The SQL SELF JOIN is used to join a table to itself as if the table were two tables,
temporarily renaming at least one table in the SQL statement.
Syntax:
SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;
Example:
SQL> SELECT a.ID, b.NAME, a.SALARY
FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY < b.SALARY;
36.CARTESIAN JOIN
The CARTESIAN JOIN or CROSS JOIN returns the cartesian product of the sets of records
from the two or more joined tables.
Syntax:
SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]
Example:
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS, ORDERS;
Example :
SQL> SELECT * FROM CUSTOMERS
ORDER BY NAME, SALARY;
38. Group By :
GROUP BY clause is used in collaboration with the SELECT statement to arrange identical
data into groups
Syntax:
SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
Example:
SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP BY NAME;
Syntax:
SELECT DISTINCT column1, column2,.....columnN
FROM table_name WHERE [condition]
Example:
SQL> SELECT SALARY FROM CUSTOMERS ORDER BY SALARY;
Example:
SQL > SELECT *
FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 2;
The percent sign (%) : Matches one or more characters. Note that MS Access uses the
asterisk (*) wildcard
character instead of the percent sign (%) wildcard character
The underscore (_) : Matches one character. Note that MS Access uses a question mark (?)
instead of the
underscore (_) to match any one character.
Syntax:
The basic syntax of ‘%’ and ‘_’ is as follows:
43.Triggers
A database trigger is code that is automatically executed in response to certain events on a
n particular table in a database.
Example :
IF EXISTS (SELECT name
FROM sysobjects
WHERE name = 'CheckPhoneNumber'
AND type = 'TR')
DROP TRIGGER CheckPhoneNumber
GO
Consistency: Ensures that all changes made through successful transaction are reflected
properly on database
Isolation: Ensures that all transactions are performed independently and changes made by
one transaction are not reflected on other
Durability: Ensures that the changes made in the database with committed transactions
persist as it is even after a system failure.
Oracle works to efficiently manage its resource, a database of information, among the
multiple clients requesting and sending data in the network.
It is an excellent database server choice for client/server computing. Oracle supports all
major operating systems for both clients and servers, including MSDOS, NetWare,
UnixWare, OS/2 and most UNIX flavors.