SQL Interview Questions
SQL Interview Questions
1. What is database?
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 designs 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
software.
3. What is RDBMS? How is it different from DBMS?
RDBMS stands for Relational Database Management System. The key difference
here, compared to DBMS, is that RDBMS stores data in the form of a collection
of tables, and relations can be defined between the common fields of these tables.
Most modern database management systems like MySQL, Microsoft SQL Server,
Oracle, IBM DB2, and Amazon Redshift are based on RDBMS.
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.
5. What is the difference between SQL and MySQL?
SQL is a standard language for retrieving and manipulating structured databases.
On the contrary, MySQL is a relational database management system, like SQL
Server, Oracle, or IBM DB2, that is used to manage SQL databases.
6. What are Tables and Fields?
A table is an organized collection of data stored in the form of rows and columns.
Columns can be categorized as vertical and rows as horizontal. The columns in a
table are called fields while the rows can be referred to as records.
7. What are Constraints in SQL?
Constraints are used to specify the rules concerning data in the table. It can be
applied for single or multiple fields in an SQL table during the creation of the table
or after creating using the ALTER TABLE command. The constraints are:
NOT NULL - Restricts NULL value from being inserted into a column.
CHECK - Verifies that all values in a field satisfy a condition.
DEFAULT - Automatically assigns a default value if no value has been
specified for the field.
UNIQUE - Ensures unique values to be inserted into the field.
INDEX - Indexes a field providing faster retrieval of records.
PRIMARY KEY - Uniquely identifies each record in a table.
FOREIGN KEY - Ensures referential integrity for a record in another table.
The PRIMARY KEY constraint uniquely identifies each row in a table. It must
contain UNIQUE values and has an implicit NOT NULL constraint.
A table in SQL is strictly restricted to have one and only one primary key, which
is comprised of single or multiple fields (columns).
A UNIQUE constraint ensures that all values in a column are different. This
provides uniqueness for the column(s) and helps identify each row uniquely.
Unlike primary key, there can be multiple unique constraints defined per table.
The code syntax for UNIQUE is quite like that of PRIMARY KEY and can be
used interchangeably.
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;
Cross join can be defined as a cartesian product of the two tables included in the
join. The table after join contains the same number of rows as in the cross-
product of the number of rows in the two tables. If a WHERE clause is used in
cross join, then the query will work like an INNER JOIN.
Data Integrity is the assurance of accuracy and consistency of data over its entire
life cycle and is a critical aspect of the design, implementation, and usage of any
system which stores, processes, or retrieves data. It also defines integrity
constraints to enforce business rules on the data when it is entered into an
application or a database.
SELECT operator in SQL is used to select data from a database. The data
returned is stored in a result table, called the result-set.
TRUNCATE command is used to delete all the rows from the table and free the
space containing the table.
DROP command is used to remove an object from the database. If you drop a
table, all the rows in the table are deleted and the table structure is removed from
the database.
If a table is dropped, all things associated with the tables are dropped as well.
This includes - the relationships defined on the table with other tables, the
integrity checks and constraints, access privileges and other grants that the table
has. To create and use the table again in its original form, all these relations,
checks, constraints, privileges, and relationships need to be redefined. However,
if a table is truncated, none of the above problems exist and the table retains its
original structure.
The TRUNCATE command is used to delete all the rows from the table and free
the space containing the table.
The DELETE command deletes only the rows from the table based on the
condition given in the where clause or deletes all the rows from the table if no
condition is specified. But it does not free the space containing the table.
Entity: An entity can be a real-world object, either tangible or intangible, that can
be easily identifiable. For example, in a college database, students, professors,
workers, departments, and projects can be referred to as entities. Each entity has
some associated properties that provide it an identity.