1/129
Looks like no tags are added yet.
First paper detailing SEQUEL happened when?
1974
The first paper detailing SEQUEL was published by who?
IBM
Who were IBM Researchers?
Donald J Chamberlin + Raymond F Boyce
SEQUEL stands for
Structure English Query language
The American National Standards institue (ANSI) prescribes a standard SQL (offcially accepted)
1986
RDBMS
Relational Database Management Systems
Different RDBMS (MySQL, PostgreSQL, Oracle, and SQL Server) follow the SAME _____ with ______ differences
ANSI standards, minor syntax
Relational Databases + Sql = built on strong mathematical principles, such as
set theory, relational algebra, and relational calculus.
Set theory
Relational databases treat tables as sets of tuples, which ensures data consistency.
Tuples
Rows
SQL is a _______ language - it allows developes to SPECIFY their ______ _______ without DETAILING EACH ______ ______ execution process
Declarative, desired results, step-by-step
DCL
Data Control Language
What does DCL do?
a subset of SQL commands used to manage security by controlling user access to database objects like tables, views, and procedures
DCL allows administrators to
GRANT, REVOKE user access permission
GRANT
Assigns specific privileges to users, allowing them to perform actions such as SELECT, INSERT, UPDATE, or DELETE on database objects.
REVOKE
Removes previously granted or denied privileges from users, taking away their access rights.
Example: REVOKE UPDATE ON Employees FROM 'HR_user';
Main Purpose of DCL?
access management - Controlling which users can access data and to what extent.
security - Protecting sensitive information from unauthorized access
role-based access - Often used in conjunction with user roles to assign permissions to groups of users efficiently
DBA
Database Administrators
DCL is _______ for database security and is generally managed by ______ to ensure data integrity and confidentiality.
crucial, DBA’s.
DDL
Data Definition Language
DDL is a subset of SQL that
is used to define, modify, and manage the structure of database objects such as tables, indexes, views, and schemas
DDL commands deal with the _________ of the database, not the _______ ______ within it.
blueprint/schema, data stored
Primary DDL commands
CREATE, DROP, ALTER
CREATE
Used to build a new database, table, index, view, or other object.
Example: _______ TABLE employees (id INT PRIMARY KEY, name VARCHAR(50), salary DECIMAL(10,2));
DROP
Deletes an existing object and all the data and structure associated with it permanently.
Example: _____ TABLE employees;
ALTER
This command modifies the structure of an existing database object. It can be used to add, modify, or drop columns; add or delete constraints; or rename a column or table.
Example (Add a column): ______ TABLE Customers ADD Email varchar(255);
DML
Data Manipulation Language
Data Manipulation Language (DML) is a subset of SQL commands used to
manage, modify, and retrieve data within database objects (tables/views)
What does DML focus on?
on the data itself—rather than database structure (DDL)
DML commands
INSERT, DELETE, UPDATE records
INSERT
Adds new rows of data to a table.
DELETE
Removes rows or records from a table.
UPDATE:
Modifies existing data within a table.
DML works on the _______, interacting directly with _______information rather than creating tables or schemas.
data plane, stored
DML real life
inventory updates
data analysis
user data management
DML vs DDL
DML works INSIDE the EXISTING data
DDL works on the STRUCTURE of the data
DQL
Data Query Language
Data Query Language commands
SELECT data records
Data Query Language (DQL) is a subset of SQL used to
retrieve data from a database.
SELECT
fetches data records based on the query criteria.
TCL
Transaction Control Language
TCL commands
COMMIT, ROLLBACK (Undo)
Transaction Control Language (TCL) in SQL is used to
manage database transactions, ensuring data integrity and consistency by either permanently saving changes or undoing them.
COMMIT
used to permanently save all changes made during the current transaction to the database.the changes are irreversible and become visible to all other database users and sessions
ROLLBACK
used to undo all changes made during the current transaction that have not yet been committed. This restores the database to its state before the transaction began or to the last savepoint.
TCL commands enforce the ___ properties
ACID
ACID STANDS FOR?
Atomicity - Ensures that all operations within a transaction are treated as a single, indivisible unit
Consistency - Guarantees that a transaction transforms the database from one valid state to another
Isolation - Ensures that concurrent execution of transactions leaves the database in the same state as if they were executed sequentially, preventing intermediate, uncommitted data from being visible to other transactions.
Duration - Guarantees that once a transaction is committed, its changes are permanently stored in the system, even in the event of a power loss, crash, or error.
Using a small data type for rapidly growing data can lead to severe data integrity issues such as :
truncation, rounding
truncation
loss of precision/data
rounding
the small difference between an exact number and its rounded approximation
Though small data types help with space, they can also cause _________ issues
memory + performance
storing variable length data in ______ instead of ______ actually WASTES storage space since _______ uses FULL FIXED LENGHT
CHAR, VARCHAR, CHAR
CHAR
Fixed length, column always reserves and uses a fixed amount of storage space equal to the defined length n for every entry, regardless of the actual length of the data inserted. Shorter strings are padded with trailing spaces to meet the fixed length, which consumes unnecessary disk space if the data length varies significantly.
VAR CHAR
What it is: Variable-length string (non-Unicode)
Max size: Up to 8,000 characters
Storage: 1 byte per character
Use when: You only need standard English/ASCII text
VARCHAR is significantly more effective since
it takes up space EFFICENTLY, doesnt waste, doesnt take up unnecessary amount
Storing numeric values/dates in VARCHAT can cause issues with
data integrity
formatting
sorting efficiency
Database systems handle VARCHAR data as
texts, not numeric or temporal types
NVARCHAR
What it is: Variable-length string with Unicode support
Max size: Up to 4,000 characters
Storage: 2 bytes per character
Use when: You need to support multiple languages, emojis, or special characters
NVARCHAR vs VARCHAR
one stores non-Unicode (ASCII) data, using 1 byte per character, making it ideal for saving space. for efficency
the other stores Unicode data, using 2 bytes per character, supporting multilingual text. for internationalization,
VARCHAR(max)
What it is: Variable-length string for very large text
Max size: Up to 2 GB (~2 billion characters)
Storage: 1 byte per character
Use when: Storing long text like:
Articles
Logs
Descriptions
using NVARCHAR or VARCHAR(MAX) for all columns wastes storage space and can ______ impact query performance
negatively
Good design =
tight
efficient
scalable
Delete and update statements only apply to ______ table at a time
1
Need multiple languages or emojis →
Small/medium English-only text →
Huge text blobs →
NVARCHAR, VARCHAR(n), VARCHAR(MAX)
A single _______ command cannot modify multiple tables simultaneously.
UPDATE
referential integrity
If one table depends on another, you can’t delete the “parent” record while the “child” still exists.
Think of a table like a
folder
Drop Table =
deleting the whole folder
Dropping a table deletes:
Table Structure
All Data
Indexes
Truncate Table
Empties the folder (wipe it clean but keep the table)
Trunucate Table deletes _________ but keeps _______ and also_____
all rows, table structure, resets auto increments (ID start back at 1)
DELETE TABLE
remove specific files in a table (choose what to remove)
DELETE table delects _______ keeps ________ but doesn’t _______
selected rows, table structure, resets auto-increments
_____ (inner) → smallest impact (one/some rows)
_______ (middle) → bigger (all rows)
______ (outer) → biggest (table + everything)
DELETE, TRUNCATE, DROP
You can’t ________ or _______ if other tables depend on it
TRUNCATE
DROP
SQL Query follows this order:
SELECT —— FROM —- WHERE ——- GROUP BY ——- HAVING ——- ORDER BY. (instruction to get data)
SELECT =
What do you want? (______ name, age)
FROM =
Which table are you getting it from?
WHERE=?
choose a specific row ( _______ age>18)
GROUP BY =
used when you want summaries (like counts, average) (______ by major)
HAVING =
filter groups (after grouping)
ORDER BY =
sorting the results
SELECT major, COUNT(*)
FROM Students
WHERE age > 18
GROUP BY major
HAVING COUNT(*) > 5
ORDER BY COUNT(*) DESC;
ranslation:
Get majors and how many students are in each
Only students older than 18
Group by major
Only keep majors with more than 5 students
Sort from biggest to smallest
SELECT *
“Give me ALL columns” Select all columns
SELECT * should be avoided and only used for small data since
It pulls unnecessary data → slower
Uses more memory
Bad practice for big tables
WHERE = _____ grouping
HAVING = _____ grouping
before, after
SQL is written like this:
SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY👉 BUT it actually runs like this:
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY“Which runs first?”
FROM
2. “Why can’t you use alias in WHERE?”
Because SELECT hasn’t happened yet
Which comes last?
ORDER BY
FWGHSO
FROM → WHERE → GROUP → HAVING → SELECT → ORDER
COUNT =
how many things are there
So what is COUNT actually counting?
It counts rows (records)
COUNT(*) →
counts everything (all rows)
COUNT(column) →
counts only NON-null values, count only filled cells
COUNT(*) = COUNT(column)if there are NO NULL
DISTINC
= remove duplicates
DISTINCT applies to the
whole row combination
DISTINCT often appears _______ BUT you should always use ORDER BY if you care about order
sorted
DISTINCT can be slow because:
It has to compare rows
It has to remove duplicates