7 Days Analytics Course 3feiz7 1
7 Days Analytics Course 3feiz7 1
SQL (Structured Query Language) is a domain-specific language used for managing and
manipulating data in relational database management systems (RDBMS). Its different
components include Data Definition Language (DDL), Data Manipulation Language (DML), Data
Control Language (DCL), and Transaction Control Language (TCL).
SQL is a language used to manage and manipulate data in a database, whereas MySQL is an
open-source relational database management system that uses SQL as its language.
The different types of SQL commands include DDL (Data Definition Language), DML (Data
Manipulation Language), DCL (Data Control Language), and TCL (Transaction Control
Language).
A primary key is a column or group of columns that uniquely identifies each row in a table. It
must contain unique values and cannot have NULL values.
A foreign key is a column or a group of columns in a table that references the primary key of
another table. It establishes a link between the data in the two tables.
The main difference between CHAR and VARCHAR data types is that CHAR is a fixed-length
character data type, whereas VARCHAR is a variable-length character data type.
DELETE is a DML command used to remove specific rows from a table, while TRUNCATE is a
DDL command used to remove all rows from a table.
4
UNION is used to combine the result sets of two or more SELECT statements and removes
duplicate rows, whereas UNION ALL also combines the result sets but retains all rows, including
duplicates.
An index is a data structure that improves the speed of data retrieval operations on a database
table. It is used to quickly locate data without having to search every row in a database table.
A stored procedure is a prepared SQL code that can be saved and reused. It allows you to
group multiple SQL statements into a single unit and execute them whenever required.
A view in SQL is a virtual table that is based on the result set of a SELECT statement. It
contains rows and columns similar to a real table but does not store any data of its own.
Normalization is the process of organizing data in a database to reduce data redundancy and
improve data integrity. It helps in eliminating data anomalies and inconsistencies.
14. What is the difference between INNER JOIN and OUTER JOIN?
An INNER JOIN returns only the rows that have matching values in both tables, while an
OUTER JOIN returns all the rows from one or both of the tables being joined, based on whether
the condition is met or not.
The different types of joins in SQL include INNER JOIN, LEFT JOIN (or LEFT OUTER JOIN),
RIGHT JOIN (or RIGHT OUTER JOIN), and FULL JOIN (or FULL OUTER JOIN).
5
A subquery is a query nested within another SQL query. It is used to return data that will be
used in the main query as a condition to further restrict the data to be retrieved.
A self-join is a regular join, but the table is joined with itself. It is particularly useful when a table
has a foreign key that references its own primary key.
18. What is the difference between a primary key and a unique key?
A primary key is a column or group of columns that uniquely identifies each row in a table and
does not allow NULL values, while a unique key ensures that all values in a column are
different. Unlike the primary key, it allows NULL values.
19. How to find the second highest salary from an employee table?
To find the second highest salary from an employee table, you can use the following SQL query:
SELECT MAX(salary)
FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee);
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that guarantee the
reliability of database transactions. Atomicity ensures that all operations within a transaction are
completed successfully; Consistency ensures that the database remains in a valid state before
and after the transaction; Isolation ensures that the concurrent execution of transactions results
in a system state that would be obtained if transactions were executed serially; and Durability
ensures that once a transaction has been committed, it will remain so even in the case of
system failure.
A trigger in SQL is a set of SQL statements that automatically "fires" or executes when a
specified event occurs, such as when data is modified in a table.
6
A function in SQL is a set of SQL statements that can accept input parameters, perform actions,
and return a value. A stored procedure, on the other hand, can perform a series of SQL
operations and may or may not return a value.
A clustered index determines the physical order of data in a table, while a non-clustered index
does not alter the physical order of the table and creates a separate structure to store the
indexed columns.
The GROUP BY clause is used in SQL to group rows that have the same values into summary
rows, such as "sum," "count," "avg," "min," or "max" to apply functions to each group.
The HAVING clause is used in combination with the GROUP BY clause to filter records that are
returned by a GROUP BY clause.
A composite key is a combination of two or more columns that uniquely identifies each row in a
table. Each column may not be unique by itself, but the combination of columns is unique.
A Common Table Expression (CTE) is a temporary result set that can be referenced within the
context of a SELECT, INSERT, UPDATE, or DELETE statement.
Data integrity in SQL refers to the accuracy and consistency of data stored in a database. It
ensures that data remains accurate, reliable, and accessible over time.
A constraint in SQL is a rule that is enforced on the data in a table. It can be used to enforce
data integrity and to ensure that data meets certain criteria.
7
30. What is the difference between a candidate key and a composite key?
A candidate key is a column or a set of columns that can uniquely identify any row in a table,
while a composite key is a combination of two or more columns that can uniquely identify any
row in a table.
A cursor is a database object that allows traversal over the rows of a result set, while a trigger is
a set of SQL statements that automatically execute or fire when a specified event occurs.
The different types of constraints in SQL include NOT NULL, UNIQUE, PRIMARY KEY,
FOREIGN KEY, and CHECK constraints.
33. What is the difference between a unique constraint and a primary key?
A unique constraint ensures that all values in a column are different, while a primary key is a
column or a set of columns that uniquely identifies each row in a table and does not allow NULL
values.
A clustered index determines the physical order of data in a table, while a non-clustered index
does not alter the physical order of the table and creates a separate structure to store the
indexed columns.
A LEFT JOIN returns all rows from the left table and the matched rows from the right table, while
a RIGHT JOIN returns all rows from the right table and the matched rows from the left table.
A natural join in SQL is a type of join that combines columns with the same name in both tables,
eliminating duplicate columns.
An inner join is a join that returns only the rows with matching values in both tables, while a
self-join is a join that joins a table with itself.
8
A cross join in SQL is a join that produces the Cartesian product of the two tables involved.
A temporary table in SQL is a table that exists temporarily and holds a subset of data from
another table. It is automatically dropped when the session that created it ends.
A temporary table is a physical table that exists for the duration of a session, while a table
variable is a variable that can hold a set of data and exists for the duration of a batch,
procedure, or function.
A stored procedure is a set of SQL statements that can be saved and reused, while a
user-defined function is a set of SQL statements that returns a value and can be used in SQL
statements wherever expressions are allowed.
A view in SQL is a virtual table based on the result set of a SELECT statement, while a table is
a collection of data organized into rows and columns.
The ORDER BY clause is used to sort the result set of a SELECT statement in either ascending
or descending order based on one or more columns.
The DISTINCT keyword in SQL is used to retrieve unique values from a column or set of
columns in a table.
The BETWEEN operator in SQL is used to retrieve values within a specific range.
What is the difference between the LIKE and the IN operator in SQL?
The LIKE operator is used for pattern matching, while the IN operator is used to specify multiple
values in a WHERE clause.