SQL Questions
SQL Questions
SQL Questions
1. What is SQL?
SQL - Structured Query Language and is used to communicate with relational databases
Unique identifier for each record in a table. It ensures that each row in the table has a distinct and
non-null value in the primary key column. (create relationships between tables)
It is a field in one table referencing the primary key in another. It establish connection between
tables.
JOIN operation merges information from two or more tables by utilizing a common column that
links them together.
5. What do you mean by a NULL value in SQL?
Unique key guarantees that every value in a column (or a combination of columns) remains
distinct and cannot be replicated within a table.
Schema fixă: Aceste baze de date necesită o schemă fixă și predefinită, care descrie
structura datelor. Schimbările de schemă pot fi dificile și necesită adesea modificări
complexe.
Structured Data
Diverse modele de date: NoSQL acoperă o gamă largă de modele de date, inclusiv
documente, chei-valoare, coloane și grafuri. Acestea sunt adaptate pentru diverse tipuri
de date și aplicații.
A constraint in SQL defines rules or restrictions that apply to data in a table, ensuring data
integrity. Common constraints include:
Fn1 – avem primary key si toate elementele din tabel sunt atomice, in fiecare celula avem doar o
valoare
Fn2 – fn1 + coloană care nu face parte din cheia primară trebuie să depindă de întreaga cheie
primară, nu doar de o parte a acesteia. (coloanele care nu sunt chei primare depind de toate
componentele cheii primare.)
Fn3 – fn2+ fiecare coloană care nu face parte din cheia primară trebuie să depindă direct de cheia
primară, nu de alte coloane din tabelă. (o coloană nu trebuie să depindă de alte coloane care nu
sunt cheia primară.)
Indexes improve the data retrieval operations speed. They provide a quick way to locate specific
rows in a table. Indexes are essential for optimizing query performance.
Haveing – este utilizat in acelasi context cu group by si este pozitionat dupa acesta
View este o tabele virtuala care stocheaza rezultatul unui query SQL.
FROM Angajati
@EmployeeID INT
AS
BEGIN
END;
Trigger is a special type of stored procedure that automatically executes in response to certain
database events or actions.
Aggregate functions in SQL perform calculations on a set of values and return a single result.
(MIN, MAX, SUM, COUNT)
INTERMEDIARE
17. What is a self-join, and how would you use it?
A self-join is a type of join where a table is joined with itself. It is useful when creating
relationships within the same table, such as finding hierarchical relationships or comparing rows
with related data.
SELECT name
FROM employees
UNION
RESULT:
UNION merges the outcomes of two or more SELECT statements, removing duplicate rows,
whereas UNION ALL merges the results without removing duplicates. While UNION ALL is
faster, it may include duplicate rows.
The ACID properties are a set of four characteristics that guarantee the reliability and
consistency of database transactions:
1. Atomicity: Transactions are atomic, meaning they are either executed in their entirety or
not at all. If any part of a transaction fails, the entire transaction is rolled back to its initial
state, ensuring that the database remains in a consistent state.
2. Consistency: Refers to the assurance that a transaction can only bring the database from
one valid state to another valid state. In simpler terms, it means that the data in the
database remains in a valid and expected state at all times, both before and after the
execution of a transaction.
EXEMPLE when data is not valid: have rows where data is missing, insert a value (a
record) which have the same primary key value with an existent one
3. Isolation: Transactions operate independently of each other, and their effects are not
visible to other transactions until they are committed. This ensures that concurrent
transactions do not interfere with each other and that each transaction sees a consistent
view of the database.
4. Durability: Once a transaction is committed, its changes are permanently saved in the
database and cannot be lost, even in the event of a system failure. This ensures that the
database can recover its data to a consistent state after a crash or restart.
Data integrity - refers to the accuracy, consistency, and reliability of data stored in a database. It
ensures that the data remains valid.
Inconsistency - refers to a state where data does not conform to the expected or desired state.
24. How does a clustered index work and how is it different
from a non-clustered index?
Clustered Index:
In a clustered index, the physical order of the rows in the table is the same as the order of
the index keys.
Each table can have only one clustered index because the data rows themselves are stored
in the order of the clustered index keys.
When a clustered index is created on a table, the table's data is physically reorganized to
match the order specified by the index keys.
Non-Clustered Index:
In a non-clustered index, the index keys are stored separately from the actual data rows in
the table.
Each table can have multiple non-clustered indexes, allowing for flexibility in indexing
different columns.
Non-clustered indexes store pointers to the location of the actual data rows, rather than
storing the data itself.
Data integrity in SQL is ensured through various means, including constraints (e.g., primary
keys, foreign keys, check constraints), normalization, transactions, and referential integrity
constraints. These mechanisms prevent invalid or inconsistent data from being stored in the
database.
SQL injection is a type of security vulnerability that occurs when an attacker is able to inject
malicious SQL code into a query, thereby gaining unauthorized access to the database or
manipulating its contents