VND - Openxmlformats Officedocument - Wordprocessingml.document&rendition 1 3
VND - Openxmlformats Officedocument - Wordprocessingml.document&rendition 1 3
1. Declarative Nature: SQL allows users to specify what data they want without
detailing how to retrieve it. The database management system (DBMS) handles the
execution.
2. Structured Query Format: SQL uses a structured syntax with commands like
SELECT, INSERT, UPDATE, DELETE, and CREATE for data manipulation and
definition.
3. Relational Database Support: Designed for relational databases, SQL operates on
tables with rows and columns, supporting relationships via keys (primary, foreign).
4. Portability: SQL is standardized (e.g., ANSI/ISO standards), making it compatible
across various DBMS like MySQL, PostgreSQL, Oracle, and SQL Server.
5. Data Integrity: Supports constraints (e.g., NOT NULL, UNIQUE, FOREIGN KEY)
to ensure data accuracy and consistency.
6. Scalability: Handles small to large datasets efficiently, with optimization for complex
queries.
7. Data Manipulation and Definition: Includes Data Query Language (DQL), Data
Manipulation Language (DML), Data Definition Language (DDL), and Data Control
Language (DCL) for querying, modifying, defining, and controlling access.
8. Non-Procedural: Focuses on the result rather than the process, unlike procedural
languages like C or Java.
9. Client-Server Architecture: SQL is often used in client-server systems, allowing
multiple users to access databases concurrently.
10. Extensibility: Supports stored procedures, triggers, and user-defined functions for
advanced functionality.
Advantages of SQL
SQL data types define the type of data that can be stored in a column of a database table.
Different database management systems (DBMS) like MySQL, PostgreSQL, Oracle, and
SQL Server may have slight variations in data type names and specifications, but the core
categories are standardized. Below are the main categories of SQL data types with common
examples:
Exact Numeric:
o INT or INTEGER: Whole numbers (e.g., 42, -10).
o SMALLINT: Smaller range integers (e.g., -32,768 to 32,767 in many DBMS).
o BIGINT: Larger range integers (e.g., -2^63 to 2^63-1).
o DECIMAL or NUMERIC(p, s): Fixed-point numbers with precision p (total digits)
and scale s (decimal places) (e.g., 123.45 for NUMERIC(5,2)).
Approximate Numeric:
o FLOAT(p): Floating-point numbers with precision p.
o REAL: Single-precision floating-point.
o DOUBLE PRECISION: Double-precision floating-point.
BOOLEAN: Stores TRUE, FALSE, or NULL (supported in some DBMS like PostgreSQL;
others use integers or other types to simulate).
Note: Specific data types and their sizes may vary by DBMS. For example, MySQL uses
TINYINT for small integers, while PostgreSQL uses SERIAL for auto-incrementing integers.
SQL Literals
Literals are fixed values explicitly written in SQL queries to represent data. They are used in
statements like SELECT, INSERT, or WHERE clauses. SQL supports several types of
literals, corresponding to data types:
1. Numeric Literals
2. String Literals
3. Boolean Literals
Represent temporal values, often enclosed in single quotes and formatted according to the
DBMS.
5. Binary Literals
6. NULL Literal
7. Other Literals
JSON: In DBMS supporting JSON, literals are written as JSON strings, e.g., '{"name":
"John", "age": 30}'.
Interval: Represents time durations, e.g., INTERVAL '1 day' (PostgreSQL-specific).
Example: SELECT '{"key": "value"}'::JSON AS json_data; (PostgreSQL).
DDL commands define and modify the structure of database objects like tables, schemas, and
indexes.
DML commands manage the data within tables, allowing insertion, updating, deletion, and
retrieval.
DCL commands manage access permissions and security levels for database objects.
DQL commands are used to query and retrieve data from the database. Technically, DQL is a
subset of DML, but it’s often listed separately due to its focus on data retrieval.
Notes
SQL operators are special symbols or keywords used to perform operations on data within
SQL queries, such as comparisons, arithmetic, logical operations, and more. They are integral
to constructing queries for filtering, calculating, and combining data in relational databases.
Below is a concise overview of the main types of SQL operators, their purposes, and how
they are used (their "procedure").
1. Arithmetic Operators
Operators:
o +: Addition (e.g., 5 + 3 = 8)
o -: Subtraction (e.g., 5 - 3 = 2)
o *: Multiplication (e.g., 5 * 3 = 15)
o /: Division (e.g., 6 / 2 = 3)
o %: Modulus (remainder of division, e.g., 7 % 2 = 1)
Procedure:
o Used in SELECT, WHERE, or other clauses to compute values.
o Operands are typically numeric columns, literals, or expressions.
o Example: SELECT salary * 1.1 AS new_salary FROM employees;
Increases salary by 10% and returns the result as new_salary.
o Example: SELECT id, quantity % 10 AS remainder FROM inventory;
Returns the remainder of quantity divided by 10.
2. Comparison Operators
Operators:
o =: Equal to
o != or <>: Not equal to
o <: Less than
o >: Greater than
o <=: Less than or equal to
o >=: Greater than or equal to
Procedure:
o Compare columns, literals, or expressions to filter rows.
o Often used in WHERE to define conditions.
o Example: SELECT name FROM employees WHERE salary > 50000;
Retrieves names of employees with a salary greater than 50,000.
o Example: SELECT product FROM items WHERE price != 0;
Returns products with non-zero prices.
3. Logical Operators
Operators:
o AND: True if all conditions are true.
o OR: True if at least one condition is true.
o NOT: Negates a condition.
Procedure:
o Used in WHERE or HAVING to combine conditions.
o Example: SELECT name FROM employees WHERE dept = 'IT' AND salary >
60000;
Retrieves names of employees in the IT department with a salary above
60,000.
o Example: SELECT name FROM employees WHERE dept = 'HR' OR dept = 'Sales';
Retrieves names of employees in either HR or Sales.
o Example: SELECT name FROM employees WHERE NOT dept = 'IT';
Retrieves names of employees not in the IT department.
4. Concatenation Operator
Operator:
o || (standard SQL, used in PostgreSQL) or + (in SQL Server).
o Some DBMS (e.g., MySQL) use CONCAT() function instead.
Procedure:
o Combines string literals, columns, or expressions into a single string.
o Example: SELECT first_name || ' ' || last_name AS full_name FROM employees;
Concatenates first_name and last_name with a space (PostgreSQL).
o Example (MySQL): SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;
Same result using MySQL’s CONCAT() function.
5. Bitwise Operators
Operators:
o &: Bitwise AND
o |: Bitwise OR
o ^: Bitwise XOR
o ~: Bitwise NOT (DBMS-specific)
Procedure:
o Used for low-level operations on binary data (less common in typical SQL queries).
o Example: SELECT 5 & 3 AS result; (Result: 1, as 5 = 0101 and 3 = 0011, so 0101 &
0011 = 0001).
o Supported in DBMS like PostgreSQL, MySQL, and SQL Server.
6. Set Operators
Operators:
o UNION: Combines rows from two queries, removing duplicates.
o UNION ALL: Combines rows, keeping duplicates.
o INTERSECT: Returns rows common to both queries.
o EXCEPT (or MINUS in Oracle): Returns rows in the first query not in the second.
Procedure:
o Used between two SELECT statements with compatible column types and counts.
o Example: SELECT name FROM employees WHERE dept = 'IT' UNION SELECT
name FROM contractors WHERE dept = 'IT';
Combines unique names from employees and contractors in the IT
department.
o Example: SELECT id FROM orders INTERSECT SELECT id FROM returns;
Returns IDs present in both orders and returns (supported in PostgreSQL,
SQL Server).
7. Special Operators
Operators:
o LIKE: Matches patterns in strings (uses % for zero or more characters, _ for a single
character).
Example: SELECT name FROM employees WHERE name LIKE 'A%';
Retrieves names starting with 'A'.
o IN: Checks if a value is in a list.
Example: SELECT name FROM employees WHERE dept IN ('IT', 'HR');
Retrieves names of employees in IT or HR.
o BETWEEN: Checks if a value is within a range (inclusive).
Example: SELECT name FROM employees WHERE salary BETWEEN
40000 AND 60000;
Retrieves names of employees with salaries between 40,000 and
60,000.
o IS NULL / IS NOT NULL: Checks for NULL values.
Example: SELECT name FROM employees WHERE phone IS NULL;
Retrieves names of employees with no phone number.
o EXISTS: Tests for the existence of rows in a subquery.
Example: SELECT name FROM departments WHERE EXISTS (SELECT 1
FROM employees WHERE dept_id = departments.id);
Retrieves departments with at least one employee.
1. Identify the Context: Determine where the operator is needed (e.g., SELECT for
calculations, WHERE for filtering, or between SELECT statements for set operations).
2. Choose the Operator: Select the appropriate operator based on the operation (e.g., + for
arithmetic, AND for logical conditions, LIKE for pattern matching).
3. Construct the Query:
o Ensure operands (columns, literals, or expressions) are compatible with the operator
(e.g., numeric for arithmetic, strings for LIKE).
o Place the operator in the correct clause (e.g., WHERE for comparisons, SELECT for
arithmetic or concatenation).
4. Test and Validate: Run the query and verify the results, ensuring the operator behaves as
expected (e.g., check for case sensitivity in LIKE or NULL handling).
5. DBMS-Specific Considerations: Check for DBMS-specific syntax (e.g., || vs. CONCAT(),
or EXCEPT vs. MINUS in Oracle).
Notes
Operator Precedence: SQL follows a precedence order (e.g., arithmetic before comparison,
AND before OR). Use parentheses to enforce specific evaluation order, e.g., WHERE (salary
> 50000 AND dept = 'IT') OR dept = 'HR'.
DBMS Variations: Some operators (e.g., EXISTS, INTERSECT, EXCEPT) may not be
supported in all DBMS (e.g., MySQL lacks INTERSECT and EXCEPT).
NULL Handling: Operators involving NULL often yield NULL (e.g., 5 + NULL = NULL).
Use IS NULL instead of = NULL for comparisons.
1. Tables
Definition
A table is the primary structure for storing data in a relational database, organized in rows
and columns, where each column has a specific data type, and each row represents a record.
Characteristics
Structure: Consists of columns (attributes) with defined data types (e.g., INT, VARCHAR)
and rows (data entries).
Primary Storage: Directly stores the actual data.
Constraints: Supports constraints like PRIMARY KEY, FOREIGN KEY, NOT NULL,
UNIQUE, and CHECK to ensure data integrity.
Persistence: Data persists until explicitly modified or deleted.
Creation (DDL):
sql
o Creates a table employees with columns id, name, salary, and dept_id.
Usage (DML):
o Insert: INSERT INTO employees (id, name, salary, dept_id) VALUES (1, 'Alice',
60000, 10);
o Query: SELECT name, salary FROM employees WHERE dept_id = 10;
o Update: UPDATE employees SET salary = 65000 WHERE id = 1;
o Delete: DELETE FROM employees WHERE id = 1;
Advantages
2. Views
Definition
A view is a virtual table derived from a query on one or more tables or other views. It does
not store data physically but provides a dynamic, query-based representation of data.
Characteristics
Virtual: Does not store data; it’s a saved SELECT query executed when accessed.
Dynamic: Reflects changes in the underlying tables automatically.
Security: Can restrict access to specific columns or rows (e.g., hide sensitive data).
Simplification: Simplifies complex queries by encapsulating them.
Updatable: Some views (e.g., simple views based on one table) can be updated, depending
on the DBMS.
Creation (DDL):
sql
Advantages
Limitations
3. Indexes
Definition
An index is a database structure that improves the speed of data retrieval operations on a table
at the cost of additional storage and maintenance overhead.
Characteristics
Creation (DDL):
sql
sql
Advantages
Limitations
Comparison
Feature Table View Index
Virtual representation of
Purpose Stores actual data Optimizes data retrieval
data
Depends on query
Performance Standard access Improves read performance
complexity
1. Tables:
o Define the schema with appropriate data types and constraints.
o Use DDL to create (CREATE TABLE) and modify (ALTER TABLE, DROP
TABLE).
o Use DML for data operations (INSERT, SELECT, UPDATE, DELETE).
2. Views:
o Create with a SELECT query to encapsulate logic or restrict data.
o Query like a table; update if simple and allowed by the DBMS.
o Modify or drop as needed (CREATE OR REPLACE VIEW, DROP VIEW).
3. Indexes:
o Identify frequently queried columns (e.g., in WHERE, JOIN, or ORDER BY).
o Create indexes on those columns, balancing read vs. write performance.
o Monitor and maintain indexes (e.g., rebuild fragmented indexes in large databases).
Notes
DBMS Variations: Syntax and features vary slightly across DBMS (e.g., MySQL uses
InnoDB for clustered indexes, PostgreSQL supports materialized views for physical storage).
Performance Tuning: Indexes should be created strategically; too many can degrade write
performance.
Security with Views: Use views to hide sensitive columns (e.g., salary) while exposing non-
sensitive data (e.g., name).
Materialized Views: Some DBMS (e.g., PostgreSQL, Oracle) support materialized views,
which store data physically and need periodic refreshing.
In SQL, queries and subqueries are essential for retrieving and manipulating data from
relational databases. A query is a request for data or action, typically using the SELECT
statement, while a subquery is a query nested within another query to provide intermediate
results. Below is a concise explanation of queries and subqueries, their characteristics, types,
and how they are used.
1. Queries
Definition
A query is an SQL statement, primarily using SELECT, that retrieves data from one or more
tables or views based on specified conditions. Queries can also include other DML
commands (INSERT, UPDATE, DELETE) or combine with DDL/DCL/TCL for broader
database operations.
Characteristics
Types of Queries
1. Simple Query:
o Retrieves data from a single table.
o Example:
sql
sql
sql
Returns the average salary per department where the average exceeds 60,000.
4. Set Query:
o Combines results from multiple SELECT statements using UNION, INTERSECT, or
EXCEPT.
o Example:
sql
2. Subqueries
Definition
A subquery (or inner query) is a query nested within another query (outer query), enclosed in
parentheses. It executes first, and its result is used by the outer query to produce the final
output.
Characteristics
Nested Structure: Executes within a larger query, typically in WHERE, SELECT, FROM, or
HAVING clauses.
Single or Multi-Row: Returns a single value, single row, single column, or multiple
rows/columns, depending on its use.
Execution: The subquery runs first, and its result is passed to the outer query.
Correlation:
o Non-correlated Subquery: Independent of the outer query, runs once.
o Correlated Subquery: Depends on the outer query, runs for each row of the outer
query.
Purpose: Filters data, computes intermediate results, or checks conditions.
Types of Subqueries
1. Single-Row Subquery:
o Returns one row and one column, used with operators like =, >, <.
o Example:
sql
SELECT name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
sql
SELECT name
FROM employees
WHERE dept_id IN (SELECT id FROM departments WHERE location = 'New York');
sql
SELECT name
FROM employees e
WHERE EXISTS (SELECT 1 FROM departments d WHERE d.id = e.dept_id AND d.budget
> 100000);
sql
Returns each employee’s name and the average salary across all employees.
o Example (in FROM):
sql
1. Identify the need for a subquery (e.g., to filter based on a condition from another table or
compute an intermediate result).
2. Write the subquery to return the required data (single value, single column, or multiple rows).
3. Embed the subquery in parentheses within the outer query’s WHERE, SELECT, or FROM
clause.
4. Use appropriate operators (=, IN, EXISTS, etc.) based on the subquery’s output.
5. Test the query to ensure correctness and optimize (e.g., consider joins as alternatives for
better performance).
Output Full result set or data change Single value, row, or column(s)
Advantages of Subqueries
Performance: Correlated subqueries can be slow as they execute for each row of the outer
query.
Complexity: Nested subqueries can make queries harder to read and maintain.
Alternatives: Joins or Common Table Expressions (CTEs) may be more efficient in some
cases.
Notes
DBMS Variations: Most DBMS (e.g., MySQL, PostgreSQL, SQL Server) support
subqueries, but features like EXISTS or ALL may vary in implementation.
Optimization: Subqueries can sometimes be replaced with JOIN for better performance,
especially for non-correlated subqueries.
CTEs as Alternatives: Common Table Expressions (WITH clause) can simplify complex
subqueries:
sql
WITH dept_stats AS (
SELECT dept_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY dept_id
)
SELECT dept_id, avg_salary
FROM dept_stats
WHERE avg_salary > 60000;
Aggregate functions:
Aggregate functions in SQL perform calculations on a set of values (typically a column) and
return a single summarized result. They are commonly used with the GROUP BY clause to
compute summaries for groups of rows or with the entire result set in a query. These
functions are essential for data analysis, reporting, and summarizing large datasets.
Characteristics of Aggregate Functions
Below is a list of the most commonly used SQL aggregate functions, their purposes, and
examples:
1. COUNT
o Purpose: Counts the number of rows or non-NULL values in a column.
o Syntax: COUNT(*) (counts all rows) or COUNT(column_name) (counts non-
NULL values in the column).
o Example:
sql
sql
sql
sql
Returns the average salary per department where the average exceeds
60,000.
o Note: Ignores NULL values.
4. MIN
o Purpose: Finds the smallest value in a column (works on numeric, string, or
date/time data).
o Syntax: MIN(column_name)
o Example:
sql
SELECT MIN(salary) AS lowest_salary
FROM employees;
sql
sql
sql
sql
1. Identify the Aggregation Need: Determine what summary is required (e.g., count,
sum, average).
2. Select the Function: Choose the appropriate function (COUNT, SUM, etc.).
3. Specify the Scope:
o Use in SELECT for the result set or HAVING to filter groups.
o Combine with GROUP BY to aggregate by categories (e.g., by department).
4. Handle NULLs: Ensure the function’s NULL handling aligns with the requirement
(most ignore NULL except COUNT(*)).
5. Test and Optimize:
o Verify the output matches expectations.
o Use indexes on columns involved in GROUP BY or WHERE for better
performance.
Key Points
GROUP BY Clause:
o Required when aggregating by groups; non-aggregated columns in SELECT
must appear in GROUP BY.
o Example:
sql
HAVING Clause:
o Filters groups after aggregation (unlike WHERE, which filters rows before).
o Example:
sql
sql
SELECT name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
These are Data Manipulation Language (DML) operations used to modify data in tables.
a. INSERT
sql
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
sql
INSERT INTO table_name (column1, column2, ...) SELECT column1, column2, ... FROM
another_table WHERE condition;
Procedure:
sql
INSERT INTO employees (id, name, salary, dept_id) VALUES (1, 'Alice', 60000, 10);
sql
b. UPDATE
sql
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Procedure:
1. Identify the table and columns to update.
2. Set new values using expressions or literals.
3. Use a WHERE clause to target specific rows (omit for all rows, but use cautiously).
4. Verify the changes.
Example:
sql
sql
UPDATE employees
SET salary = (SELECT AVG(salary) FROM employees WHERE dept_id = employees.dept_id)
WHERE dept_id = 20;
c. DELETE
sql
Procedure:
1. Specify the table to delete from.
2. Use a WHERE clause to target specific rows (omit to delete all rows, but use
cautiously).
3. Verify the deletion.
Example:
sql
sql
DELETE FROM employees WHERE dept_id IN (SELECT id FROM departments WHERE location =
'Chicago');
Notes
2. Joins
Purpose: Combine rows from two or more tables based on a related column, typically used in
SELECT queries to retrieve related data.
Types:
1. INNER JOIN: Returns rows where there is a match in both tables.
2. LEFT (OUTER) JOIN: Returns all rows from the left table, with matching rows
from the right table (or NULL if no match).
3. RIGHT (OUTER) JOIN: Returns all rows from the right table, with matching rows
from the left table (or NULL if no match).
4. FULL (OUTER) JOIN: Returns all rows from both tables, with NULL for non-
matching rows.
5. CROSS JOIN: Returns the Cartesian product of both tables (all possible
combinations).
Syntax:
sql
SELECT columns
FROM table1
[JOIN_TYPE] JOIN table2
ON table1.column = table2.column;
Procedure:
1. Identify the tables and the column(s) to join on (e.g., primary key and foreign key).
2. Choose the appropriate join type based on the desired result.
3. Specify conditions in the ON clause.
4. Add WHERE, GROUP BY, or other clauses as needed.
Example (INNER JOIN):
sql
o Returns employee names and their department names for matching rows.
Example (LEFT JOIN):
sql
sql
o Returns all combinations of employee names and roles (no ON clause needed).
Notes
These are set operators that combine or compare results from multiple SELECT queries.
a. UNION
Purpose: Combines rows from two or more SELECT queries, removing duplicates.
Syntax:
sql
Procedure:
1. Ensure both SELECT queries return the same number and type of columns.
2. Combine results using UNION.
3. Use UNION ALL to include duplicates (faster, as it skips duplicate removal).
Example:
sql
b. INTERSECT
sql
Procedure:
1. Ensure column compatibility (number and type).
2. Use INTERSECT to find common rows.
Example:
sql
Purpose: Returns rows from the first SELECT query that are not in the second query.
Syntax:
sql
Procedure:
1. Ensure column compatibility.
2. Use MINUS (Oracle) or EXCEPT (PostgreSQL, SQL Server) to exclude rows.
Example:
sql
Column Compatibility: All set operators require the same number of columns with
compatible data types.
Performance: UNION ALL is faster than UNION because it doesn’t remove duplicates.
DBMS Support: INTERSECT and MINUS/EXCEPT are not universally supported (e.g.,
MySQL lacks them).
4. Cursors (PL/SQL)
Purpose: Allow row-by-row processing of query results in PL/SQL, useful for complex logic
or iterative operations.
Types:
o Implicit Cursor: Automatically managed by PL/SQL for DML operations.
o Explicit Cursor: User-defined for manual control over query results.
Syntax (Explicit Cursor):
sql
DECLARE
CURSOR cursor_name IS SELECT column1, column2 FROM table_name WHERE condition;
variable1 datatype;
variable2 datatype;
BEGIN
OPEN cursor_name;
LOOP
FETCH cursor_name INTO variable1, variable2;
EXIT WHEN cursor_name%NOTFOUND;
-- Process data
END LOOP;
CLOSE cursor_name;
END;
Procedure:
sql
DECLARE
CURSOR emp_cursor IS SELECT id, salary FROM employees WHERE dept_id = 10;
v_id employees.id%TYPE;
v_salary employees.salary%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_id, v_salary;
EXIT WHEN emp_cursor%NOTFOUND;
UPDATE employees SET salary = v_salary * 1.1 WHERE id = v_id;
END LOOP;
CLOSE emp_cursor;
END;
Notes
Performance: Cursors are slower than set-based operations (e.g., UPDATE with WHERE);
use only when row-by-row processing is necessary.
Attributes: Cursor attributes like %FOUND, %NOTFOUND, %ISOPEN, and
%ROWCOUNT provide status information.
5. Triggers (PL/SQL)
sql
Procedure:
sql
Notes
6. Procedures (PL/SQL)
Purpose: Encapsulate reusable blocks of PL/SQL code to perform specific tasks, such as data
manipulation or business logic.
Syntax:
sql
Procedure:
1. Define the procedure with optional input (IN), output (OUT), or input/output (IN
OUT) parameters.
2. Write the logic in the BEGIN block.
3. Include error handling in the EXCEPTION block.
4. Call the procedure using EXEC or CALL.
Example:
sql
sql
Notes
Summary Table
INSERT Add new rows INSERT INTO ... VALUES Add a new employee
UPDATE Modify existing rows UPDATE ... SET ... WHERE Increase salaries
Remove inactive
DELETE Remove rows DELETE FROM ... WHERE
employees
Auto-execute on DML
Triggers CREATE TRIGGER Log salary changes
events (PL/SQL)
.