SQL Guide
SQL Guide
Guide on
SQL
What is a Database?
What is a DBMS?
What is an RDBMS?
What is SQL?
What is SQL?
INT Integer (-2, 147, 483, 648 to 2, 147, 483, 647 ) INT
Note: Use CHAR for fixed-length strings and VARCHAR for variable-length strings
for better memory efficiency. Data types can also include UNSIGNED when only
positive values are required, e.g., UNSIGNED INT.
Types of SQL Commands:
Data Query Language (DQL):
Used to retrieve data from databases.
Example Command: SELECT
their objects.
Key Commands:
name VARCHAR(50),
salary DECIMAL(10, 2)
);
TRUNCATE TABLE: Removes all rows but retains the table structure.
Key Features:
Operators
=: Equal
Logical Operators:
SELECT * FROM employees WHERE city = 'New York' OR city = 'Los Angeles';
INSERT INTO employees (name, age, department) VALUES ('Alice', 30, 'HR');
COMMIT;
ROLLBACK;
SAVEPOINT before_update;
ROLLBACK TO before_update;
COMMIT;
This paraphrased content preserves the structure and
completeness of the original while introducing altered datasets
and examples.
Types of Joins:
Inner Joi
Outer Joi
Cross Joi
Self Join
Inner Join:
An inner join retrieves rows from two or more tables where the specified join
condition is satisfied. Only rows with matching values in all participating
tables
are included in the result, filtering out non-matching rows.
Syntax:
SELECT columns
FROM table1
ON
table1.column = table2.column;
Here:
Query:
FROM Customers
Result:
Customername Product
Eva Smartphone
Liam Tablet
Noah Laptop
Outer Join:
Outer joins retrieve rows from two or more tables while also including
unmatched rows. This ensures that even rows without a match are included,
Includes all rows from the left table and matching rows from the right table.
Rows from the left table without matches will have NULL values in the right
table columns.
Query:
FROM Customers
Customername Product
Eva Smartphone
Liam Tablet
Noah Laptop
Mia Null
Includes all rows from the right table and matching rows from the left table.
Rows from the right table without matches will have NULL values in the left
table columns.
Query:
FROM Customers
Result:
Customername Product
Eva Smartphone
Liam Laptop
Noah Tablet
Null Accessories
Includes all rows from both tables, with NULL values in columns
of unmatched rows.
Query:
FROM Customers
Customername Product
Eva Smartphone
Liam Tablet
Noah Laptop
Mia Null
Null Accessories
Cross Join:
A cross join generates a Cartesian product of rows from two tables, creating
all possible combinations of rows. This join does not require a specific
condition.
Syntax:
SELECT columns
FROM table1
Example: Tables
Query:
FROM Students
Customername Product
Sophia Chemistry
Sophia Biology
Jackson Chemistry
Jackson Biology
Self Join:
A self join is a join operation where a table is joined with itself. This is often
Syntax:
SELECT columns
Here:
alias1 and alias2 are aliases for the table being joined with itself
Example: Tables
Orders Table:
1 Olivia 3
2 Ethan 3
3 Emma Null
4 Lucas 3
Query:
AS Manager
FROM Employees AS e1
Employee Manager
Olivia Emma
Ethan Emma
Lucas Olivia
This paraphrased content maintains the original structure while using different
SET OPERATIONS
UNIO
INTERSEC
UNION ALL
UNION:
The UNION operator combines the result sets of two or more SELECT queries
into a single result set. It removes duplicates by default, meaning that if there
are identical rows in the result sets, only one instance of each row will appear
UNION Query:
UNION
Result:
Arya
Raj
Sneha
Arjun
INTERSECT:
The INTERSECT operator returns the common rows that exist in the result
sets of two or more SELECT queries. It only returns distinct rows that appear
INTERSECT Query:
INTERSECT
Result:
Name
(Empty)
The EXCEPT operator (also known as MINUS in some databases) returns the
distinct rows that are present in the result set of the first SELECT query but
EXCEPT Query:
EXCEPT
Result:
Name
Arya
Raj
In this example, the names "Arya" and "Raj" are students but not mentors,
UNION ALL:
The UNION ALL operator performs the same function as the UNION operator
but does not remove duplicates from the result set. It simply concatenates all
UNION ALL
Result:
Name
Arya
Raj
Sneha
Arjun
SUBQUERIES
you to use the result of one query (the inner query) as the input
for another query (the outer query). Subqueries are often used
Syntax:
SELECT columns
FROM table
In this syntax
columns refers to the specific columns you want to retrieve from the outer query
column is the column you're applying the operator to in the outer query
OPERATOR is a comparison operator such as =, >, <, IN, NOT IN, etc
(SELECT column FROM table WHERE condition) is the subquery that provides the
3 Charger 20
Subquery Example:
Retrieve the product names and quantities for orders with a total cost greater
FROM Products
ProductName Quantity
Tablet 3
Thank