Top 50 SQL Question 2
Top 50 SQL Question 2
A database is an organized collection of data, stored and retrieved digitally from a remote or
local computer system. Databases can be vast and complex, and such databases are
developed using fixed design and modeling approaches.
2. What is DBMS?
DBMS stands for Database Management System. DBMS is a system software responsible
for the creation, retrieval, updation and management of the database. It ensures that our
data is consistent, organized and is easily accessible by serving as an interface between
the database and its end users or application softwares.
4. What is SQL?
SQL stands for Structured Query Language. It is the standard language for relational
database management systems. It is especially useful in handling organized data
comprised of entities (variables) and relations between different entities of the data.
CREATE TABLE Students ( /* Create table with multiple fields as primary key */
ID INT NOT NULL
LastName VARCHAR(255)
FirstName VARCHAR(255) NOT NULL,
CONSTRAINT PK_Student
PRIMARY KEY (ID, FirstName)
);
Q => Write a SQL statement to add primary key constraint 'pk_a' for table 'table_a' and fields
'col_b, col_c'.
Q => Write a SQL statement to add a FOREIGN KEY 'col_fk' that references 'col_pk' in
'table_x'.
SELECT *
FROM Table_A
JOIN Table_B;
SELECT *
FROM Table_A
INNER JOIN Table_B;
LEFT (OUTER) JOIN: Retrieves all the records/rows from the left and the matched
records/rows from the right table.
SELECT *
FROM Table_A A
LEFT JOIN Table_B B
ON A.col = B.col;
RIGHT (OUTER) JOIN: Retrieves all the records/rows from the right and the matched
records/rows from the left table.
SELECT *
FROM Table_A A
RIGHT JOIN Table_B B
ON A.col = B.col;
FULL (OUTER) JOIN: Retrieves all the records where there is a match in either the left or
right table.
SELECT *
FROM Table_A A
FULL JOIN Table_B B
ON A.col = B.col;
Q => Write a SQL statement to CROSS JOIN 'table_1' with 'table_2' and fetch 'col_1' from
table_1 & 'col_2' from table_2 respectively. Do not use alias.
Q => Write a SQL statement to perform SELF JOIN for 'Table_X' with alias 'Table_1' and
'Table_2', on columns 'Col_1' and 'Col_2' respectively.
14. What is an Index? Explain its different types.
A database index is a data structure that provides quick lookup of data in a column or
columns of a table. It enhances the speed of operations accessing data from a database
table at the cost of additional writes and memory to maintain the index data structure.
CREATE INDEX index_name /* Create Index */
ON table_name (column_1, column_2);
Q => Write a SQL query to select the field "app_id" in table "applications" less than 1000.
Q => Write a SQL query to fetch the field "app_name" from "apps" where "apps.id" is equal
to the above collection of "app_id".
Q => Write a SQL query to fetch "names" that are present in "accounts" but not in table
"registry".
Q => Write a SQL query to fetch "names" from table "contacts" that are neither present in
"accounts.name" nor in "registry.name".
As we can observe, the Books Issued field has more than one values per record and to
convert it into 1NF, this has to be resolved into separate individual records for each book
issued. Check the following table in 1NF form -
Students Table (1st Normal Form)
Stude Salutati
Address Books Issued
nt on
Amanora Park Town Until the Day I Die (Emily
Sara Ms.
94 Carpenter)
Amanora Park Town
Sara Inception (Christopher Nolan) Ms.
94
Ansh 62nd Sector A-10 The Alchemist (Paulo Coelho) Mr.
Ansh 62nd Sector A-10 Inferno (Dan Brown) Mr.
24th Street Park
Sara Beautiful Bad (Annie Ward) Mrs.
Avenue
24th Street Park
Sara Woman 99 (Greer Macallister) Mrs.
Avenue
Ansh Windsor Street 777 Dracula (Bram Stoker) Mr.
14. Second Normal Form
A relation is in second normal form if it satisfies the conditions for first normal form and does
not contain any partial dependency. A relation in 2NF has no partial dependency, i.e., it
has no non-prime attribute that depends on any proper subset of any candidate key of the
table. Often, specifying a single column Primary Key is the solution to the problem.
Examples -
Example 1 - Consider the above example. As we can observe, Students Table in 1NF form
has a candidate key in the form of [Student, Address] that can uniquely identify all records
in the table. The field Books Issued (non-prime attribute) depends partially on the Student
field. Hence, the table is not in 2NF. To convert it into 2nd Normal Form, we will partition the
tables into two while specifying a new Primary Key attribute to identify the individual records
in the Students table. The Foreign Key constraint will be set on the other table to ensure
referential integrity.
Students Table (2nd Normal Form)
Student_I
Student Address Salutation
D
1 Sara Amanora Park Town 94 Ms.
2 Ansh 62nd Sector A-10 Mr.
3 Sara 24th Street Park Avenue Mrs.
4 Ansh Windsor Street 777 Mr.
Here, WX is the only candidate key and there is no partial dependency, i.e., any proper
subset of WX doesn’t determine any non-prime attribute in the relation.
15. Third Normal Form
A relation is said to be in the third normal form, if it satisfies the conditions for second
normal form and there is no transitive dependency between the non-prime attributes,
i.e.,all non-prime attributes are determined only by the candidate keys of the relation and
not by any other non-prime attribute.
Example 1 - Consider the Students Table in the above example. As we can observe,
Students Table in 2NF form has a single candidate key Student_ID (primary key) that can
uniquely identify all records in the table. The field Salutation (non-prime attribute), however,
depends on the Student Field rather than the candidate key. Hence, the table is not in 3NF.
To convert it into 3rd Normal Form, we will once again partition the tables into two while
specifying a new Foreign Key constraint to identify the salutations for individual records in
the Students table. The Primary Key constraint for the same will be set on the Salutations
table to identify each record uniquely.
Students Table (3rd Normal Form)
Student_I
Student Address Salutation_ID
D
1 Sara Amanora Park Town 94 1
2 Ansh 62nd Sector A-10 2
3 Sara 24th Street Park Avenue 3
4 Ansh Windsor Street 777 1
For the above relation to exist in 3NF, all possible candidate keys in above relation should
be {P, RS, QR, T}.
16. Boyce-Codd Normal Form
A relation is in Boyce-Codd Normal Form if satisfies the conditions for third normal form and
for every functional dependency, Left-Hand-Side is super key. In other words, a relation in
BCNF has non-trivial functional dependencies in the form X –> Y, such that X is always a
super key. For example - In the above example, Student_ID serves as the sole unique
identifier for the Students Table and Salutation_ID for the Salutations Table, thus these
tables exist in BCNF. Same cannot be said for the Books Table and there can be several
books with common Book Names and same Student_ID.
Q => Write a SQL query to remove first 1000 records from table 'Temporary' based on 'id'.
Q => Write a SQL statement to delete the table 'Temporary' while keeping its relations intact.