StrucStructured Query Language - SQL - Google Docs
StrucStructured Query Language - SQL - Google Docs
@bzlearnin
SQL means Structured Query Language and is used to communicate with relational
databases. It proposes a standardized way to interact with databases, allowing users
to perform various operations on the data, including retrieval, insertion, updating, and
deletion.
It is a 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. Primary keys enforce
data integrity and create relationships between tables.
4. What is a foreign key?
It is a field in one table referencing the primary key in another. It establishes a
relationship between the two tables, ensuring data consistency and enabling data
retrieval across tables.
A JOIN operation merges information from two or more tables by utilizing a common
column that links them together. Various types of JOINs exist, like INNER JOIN, LEFT
JOIN, RIGHT JOIN, and FULL JOIN. These JOIN variations dictate the manner in
which data from the involved tables is paired and retrieved.
A NULL value in SQL represents the absence of data in a column. It is not the same
as an empty string or zero; it signifies that the data is missing or unknown. NULL
values can be used in columns with optional data or when the actual data is
unavailable.
SQL databases are characterized by their use of structured tables and strict
adherence to a predefined schema, making them ideal for managing structured data
with a strong focus on data consistency and transaction support. In contrast,NoSQL
databases are non-relational and excel in handling unstructured or semi-structured
data, frequently employed for scalable, distributed, and adaptable data storage
solutions.
In SQL, a table is a structured data collection organized into rows and columns. Each
column in a table is called a field, representing a specific attribute or property of the
data.
The SELECT statement serves the purpose of fetching data from one or multiple
tables, enabling you to specify the desired columns to retrieve, apply filters through
the WHERE clause, and manage the result's sorting using the ORDER BY clause.
13. What is a constraint in SQL? Name a few.
A constraint in SQL defines rules or restrictions that apply to data in a table, ensuring
data integrity. Common constraints include:
The WHERE clause within SQL queries serves the purpose of selectively filtering
rows according to specified conditions, thereby enabling you to fetch exclusively
those rows that align with the criteria you define. For example:
Indexes improve the data retrieval operations speed. They provide a quick way to
locate specific rows in a table by creating a sorted data structure based on one or
more columns. Indexes are essential for optimizing query performance.
17. Explain GROUP BY in SQL.
The GROUP BY clause organizes rows from a table into groups based on the values
in one or more columns. It is commonly employed alongside aggregate functions like
SUM, COUNT, AVG, MIN, and MAX to perform computations on data that has been
grouped together.
An SQL alias serves as a transitory label bestowed upon either a table or a column
within a query, with the primary purpose of enhancing the clarity of query outcomes
or simplifying the process of renaming columns for improved referencing. For
example:
The ORDER BY clause is used to sort the result set of a query based on one or more
columns. You can specify each column's sorting order (ascending or descending).
For example:
The WHERE clause is employed to restrict individual rows before they are grouped,
such as when filtering rows prior to a GROUP BY operation. Conversely, the HAVING
clause is utilized to filter groups of rows after they have been grouped, like filtering
groups based on aggregate values.
21. What is a view in SQL?
An SQL view is essentially a virtual table that derives its data from the outcome of a
SELECT query. Views serve multiple purposes, including simplifying intricate queries,
enhancing data security through an added layer, and enabling the presentation of
targeted data subsets to users, all while keeping the underlying table structure
hidden.
A SQL stored procedure comprises precompiled SQL statements that can be
executed together as a unified entity. These procedures are commonly used to
encapsulate business logic, improve performance, and ensure consistent data
manipulation practices.
Aggregate functionsin SQL perform calculations ona set of values and return a
single result.
The UPDATE statement serves the purpose of altering pre-existing records within a
table. It involves specifying the target table for the update, the specific columns to be
modified, and the desired new values to be applied. For example:
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.
● INNER JOIN: Gathers rows that have matching values in both tables.
● RIGHT JOIN: Gathers all rows from the right table and any matching rows
from the left table.
● LEFT JOIN: Gathers all rows from the left table and any matching rows
from the right table.
● FULL JOIN: Gathers all rows when there's a match in either table, including
unmatched rows from both tables.
Example:
SELECT employees.name, departments.name
FROM employees
A subquery refers to a query that is embedded within another query, serving the
purpose of fetching information that will subsequently be employed as a condition or
value within the encompassing outer query. For example, to find employees with
salaries greater than the average salary:
SELECT name
FROM employees
SQL query optimization involves improving the performance of SQL queries by
reducing resource usage and execution time. Strategies include using appropriate
indexes, optimizing query structure, and avoiding costly operations like full table
scans.
It is a type of subquery that makes reference to columns from the surrounding outer
query. This subquery is executed repeatedly, once for each row being processed by
the outer query, and its execution depends on the outcomes of the outer query.
Error handling in SQL is typically achieved using try-catch blocks (in SQL Server) or
EXCEPTION blocks (in Oracle). These blocks allow you to handle and log errors
gracefully to prevent application crashes.
A clustered index in SQL determines the physical order of data rows in a table. Each
table can have only one clustered index, which impacts the table's storage structure.
Rows in a table are physically stored in the same order as the clustered index key.
SQL injection represents a security flaw that arises when SQL queries mishandle
untrusted data, posing a risk of unauthorized access or data tampering. To ward off
SQL injection, employ techniques like parameterized queries, prepared statements,
input validation, and the enforcement of stringent access controls.
40. What are the different types of triggers?
In SQL, a database schema functions as a conceptual container for housing various
database elements, such as tables, views, indexes, and procedures. Its primary
purpose is to facilitate the organization and segregation of these database elements
while specifying their structure and interconnections.
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 cybersecurity attack method that involves the insertion of
malicious SQL code into an application's input fields or parameters. This
unauthorized action enables attackers to illicitly access a database, extract
confidential information, or manipulate data.
AS
BEGIN
END;
A deadlock in SQL occurs when two or more transactions cannot proceed because
they are waiting for resources held by each other. Deadlocks can be prevented or
resolved by using techniques such as locking hierarchies, timeouts, or deadlock
detection and resolution mechanisms.
Isolation levels define the visibility of data changes one transaction makes to other
concurrent transactions. There are four commonly used isolation levels in SQL:
● READ UNCOMMITTED: At this isolation level, transactions are allowed to
read changes made by other transactions even if those changes have not
been committed. While this provides the highest level of concurrency, it
also introduces the risk of encountering dirty reads.
● READ COMMITTED: In this level, transactions can only read committed
data, avoiding dirty reads. However, it may still suffer from non-repeatable
reads and phantom reads.
● REPEATABLE READ: Transactions at this level ensure that any data read
during the transaction remains unchanged throughout the transaction's
lifetime. It prevents non-repeatable reads but may still allow phantom
reads.
● SERIALIZABLE: This represents the utmost isolation level, guaranteeing
absolute isolation between transactions. While it eradicates all
concurrency problems, it may exhibit reduced efficiency due to locking
mechanisms.
47. How does a clustered index work and how is it different from a
non-clustered index?
A clustered index defines the actual storage order of rows within a table, allowing for
only one clustered index per table and directly influencing the on-disk data
organization. Conversely, a non-clustered index does not impact the physical
arrangement of data and can coexist with multiple indexes within the same table.
● Clustered Index: When you create a clustered index on a table, the table's
rows are physically rearranged to match the order of the indexed
column(s). This makes range queries efficient but may slow down
insert/update operations.
● Non-clustered Index: Non-clustered indexes are separate data structures
that store a copy of a portion of the table's data and point to the actual data
rows. They improve read performance but come with some overhead
during data modification.
Common Table Expressions (CTEs) serve as momentary result sets that you can
mention within SQL statements, typically found within SELECT, INSERT, UPDATE, or
DELETE operations. They're established using the `WITH` keyword and are
instrumental in streamlining intricate queries by dividing them into more digestible
components.
Window functions are employed to carry out computations on a group of table rows
that are associated with the current row. They enable the generation of result sets
containing aggregated data while retaining the distinct details of each row. Typical
window functions encompass ROW_NUMBER(), RANK(), DENSE_RANK(), and SUM()
OVER().
52. What is a pivot table and how do you create one in SQL?
A pivot table is a technique used to rotate or transpose rows into columns to better
analyze and summarize data. You can create pivot tables in SQL using the `PIVOT`
operator to convert row-based data into a column-based format.
Partitioning a table involves the strategy of breaking down a sizable table into
smaller, more easily handled segments known as partitions. This method can
enhance query efficiency by permitting SQL Server to focus solely on pertinent
partitions while executing queries. Typically, partitioning is carried out using a
column characterized by a high cardinality, such as date or region.
Indexed views, or materialized views, are precomputed result sets stored as physical
tables in the database. They improve query performance by allowing the database
engine to access pre-aggregated or pre-joined data directly from the indexed view,
reducing the need for complex query processing.
Ensuring data availability and disaster recovery relies on the implementation of vital
backup and recovery strategies. These strategies encompass various methods, such
as full backups, differential backups, transaction log backups, and regular testing of
restoration procedures.
63. What are the best practices for securing a SQL database?
It is the process of copying and synchronizing data from one database to another. It
ensures data availability, load balancing, and disaster recovery. Common replication
types include snapshot replication, transactional replication, and merge replication.
65. How do you monitor SQL server performance?
Full-text search in SQL allows users to search for text-based data within large text
fields or documents. It uses advanced indexing and search algorithms to provide
efficient and accurate text-searching capabilities.
Handling big data in SQL involves dealing with large volumes of data that exceed the
capabilities of traditional database systems. Challenges include data storage,
processing, scalability, and efficient querying. Solutions may include distributed
databases and big data technologies like Hadoop and Spark.
High availability in SQL databases ensures that the database remains accessible and
operational despite failures. Techniques like clustering, replication, and failover
mechanisms help achieve high availability.
The XML data type allows to store, retrieve, and manipulateXMLdata. It provides
support for querying XML documents using XQuery, and it's commonly used in
applications that deal with XML data structures.
72. Discuss the concept of NoSQL databases and their interaction with
SQL.
NoSQL databases are non-relational databases designed for handling large volumes
of unstructured or semi-structured data. They interact with SQL databases through
various integration methods, such as data pipelines, ETL processes, and API-based
data transfers.
A spatial database stores and queries geometric and geographic data, such as
maps, GPS coordinates, and spatial objects. It provides specialized functions and
indexing methods to support spatial queries and analysis.
Advanced optimization techniques for SQL queries include using query hints,
indexing strategies, query rewriting, and understanding the query execution plan.
Profiling tools and performance monitoring are essential for identifying and resolving
performance bottlenecks.