Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

SQL Interview Questions

The document discusses various concepts related to databases and SQL. It defines terms like database, DBMS, RDBMS, SQL, tables, fields, constraints, keys, joins and aggregation functions. It also explains concepts like normalization, denormalization, views and aliases.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Interview Questions

The document discusses various concepts related to databases and SQL. It defines terms like database, DBMS, RDBMS, SQL, tables, fields, constraints, keys, joins and aggregation functions. It also explains concepts like normalization, denormalization, views and aliases.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is Database?

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 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.

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.

8. What is a Primary Key?


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).
9. What is a UNIQUE constraint?
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.

10. What is a Foreign Key?


A FOREIGN KEY comprises of single or collection of fields in a table
that essentially refers to the PRIMARY KEY in another table.
Foreign key constraint ensures referential integrity in the relation between two
tables.

11. What is a Join? List its different types.


The SQL Join clause is used to combine records (rows) from two or more tables in a
SQL database based on a related column between the two.

There are four different types of JOINs in SQL:

(INNER) JOIN:
Retrieves records that have matching values in both tables involved in the join.
This is the widely used join for queries.

LEFT (OUTER) JOIN:


Retrieves all the records/rows from the left and the matched records/rows from the
right table.

RIGHT (OUTER) JOIN:


Retrieves all the records/rows from the right and the matched records/rows from the
left table.

FULL (OUTER) JOIN:


Retrieves all the records where there is a match in either the left or right table.

12. What is a Self-Join?


A self JOIN is a case of regular join where a table is joined to itself based on
some relation between its own column(s).
Self-join uses the INNER JOIN or LEFT JOIN clause and a table alias is used to
assign different names to the table within the query.

13. What is a Cross-Join?


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.

14. What is an Index? Explain its different types.


A database index is a data structure that provides a 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.

There are different types of indexes that can be created for different purposes:

Unique Index:

Unique indexes are indexes that help maintain data integrity by ensuring
that no two rows of data in a table have identical key values.
Once a unique index has been defined for a table,
uniqueness is enforced whenever keys are added or changed within the index.

Non-Unique Index :

Non-unique indexes, on the other hand, are not used to enforce constraints
on the tables with which they are associated.
Instead, non-unique indexes are used solely to improve query performance
by maintaining a sorted order of data values that are used frequently.

Clustered and Non-Clustered Index:


Clustered indexes are indexes whose order of the rows in the database corresponds
to the order of the rows in the index.
This is why only one clustered index can exist in a given table, whereas, multiple
non-clustered indexes can exist in the table.

The only difference between clustered and non-clustered indexes is that


the database manager attempts to keep the data in the database in the
same order as the corresponding keys appear in the clustered index.

15. What is the difference between Clustered and Non-clustered index?


As explained above, the differences can be broken down into three small factors -

Clustered index modifies the way records are stored in a database based on the
indexed column.
A non-clustered index creates a separate entity within the table which references
the original table.
Clustered index is used for easy and speedy retrieval of data from the database,
whereas, fetching records from the non-clustered index is relatively slower.

16. What is Data Integrity?


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.

20. What are some common clauses used with SELECT query in SQL?

WHERE clause in SQL is used to filter records that are necessary, based on specific
conditions.

ORDER BY clause in SQL is used to sort the records based on some field(s) in
ascending (ASC) or descending order (DESC).

GROUP BY clause in SQL is used to group records with identical data


and can be used in conjunction with some aggregation functions to produce
summarized results from the database.

HAVING clause in SQL is used to filter records in combination with the GROUP BY
clause.
It is different from WHERE, since the WHERE clause cannot filter aggregated
records.

21. What are UNION, MINUS and INTERSECT commands?


The UNION operator combines and returns the result-set retrieved by two or more
SELECT statements.
The MINUS operator in SQL is used to remove duplicates from the result-set obtained

by the second SELECT query from the result-set obtained by the first SELECT query
and then return the filtered results from the first.

The INTERSECT clause in SQL combines the result-set fetched by the two SELECT
statements
where records from one match the other and then returns this intersection of
result-sets.

23. What are Entities and Relationships?


Entity: An entity can be a real-world object, either tangible or intangible, that
can be easily identifiable.
Relationships: Relations or links between entities that have something to do with
each other.

25. What is an Alias in SQL?


It is a temporary name assigned to the table or table column for the purpose of a
particular SQL query.
An alias is represented explicitly by the AS keyword but in some cases, the same
can be performed without it as well.

26. What is a View?


A view in SQL is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table.
The fields in a view are fields from one or more real tables in the database.

27. What is Normalization?


Normalization represents the way of organizing structured data in the database
efficiently.
It includes the creation of tables, establishing relationships between them, and
defining rules for those relationships.

28. What is Denormalization?


Denormalization is the inverse process of normalization,
where the normalized schema is converted into a schema that has redundant
information.
The performance is improved by using redundancy and keeping the redundant data
consistent.

33. What are Aggregate and Scalar functions?


An aggregate function performs operations on a collection of values to return a
single scalar value.
Aggregate functions are often used with the GROUP BY and HAVING clauses of the
SELECT statement.

AVG() - Calculates the mean of a collection of values.


COUNT() - Counts the total number of records in a specific table or view.
MIN() - Calculates the minimum of a collection of values.
MAX() - Calculates the maximum of a collection of values.
SUM() - Calculates the sum of a collection of values.
FIRST() - Fetches the first element in a collection of values.
LAST() - Fetches the last element in a collection of values.

A scalar function returns a single value based on the input value.

LEN() - Calculates the total length of the given field (column).


UCASE() - Converts a collection of string values to uppercase characters.
LCASE() - Converts a collection of string values to lowercase characters.
MID() - Extracts substrings from a collection of string values in a table.
CONCAT() - Concatenates two or more strings.
RAND() - Generates a random collection of numbers of a given length.
ROUND() - Calculates the round-off integer value for a numeric field (or decimal
point values).
NOW() - Returns the current date & time.
FORMAT() - Sets the format to display a collection of values.

38. What is a Stored Procedure?


A stored procedure is a subroutine available to applications that access a RDBMS.
Such procedures are stored in the database data dictionary.

DELIMITER $$
CREATE PROCEDURE FetchAllStudents()
BEGIN
SELECT * FROM myDB.students;
END $$
DELIMITER ;

39. What is a Recursive Stored Procedure?


A stored procedure that calls itself until a boundary condition is reached, is
called a recursive stored procedure.

You might also like