Sure!
Here’s a simple set of SQL (Structured Query Language) class notes:
SQL Basics - Simple Notes
1. What is SQL?
• SQL is a language used to communicate with databases.
• It is used to query, insert, update, and delete data in a database.
2. SQL Commands
SQL commands are grouped into different categories:
a. Data Query Language (DQL)
• SELECT: Retrieves data from a database.
• SELECT * FROM customers;
o * means select all columns.
o FROM customers specifies the table.
b. Data Definition Language (DDL)
• CREATE TABLE: Creates a new table in the database.
• CREATE TABLE students (
• student_id INT PRIMARY KEY,
• first_name VARCHAR(50),
• last_name VARCHAR(50),
• age INT
• );
• ALTER TABLE: Modifies an existing table (e.g., adding or removing columns).
• ALTER TABLE students ADD COLUMN email VARCHAR(100);
• DROP TABLE: Deletes a table from the database.
• DROP TABLE students;
c. Data Manipulation Language (DML)
• INSERT INTO: Adds new rows (records) into a table.
• INSERT INTO students (student_id, first_name, last_name, age)
• VALUES (1, 'John', 'Doe', 20);
• UPDATE: Modifies existing data in a table.
• UPDATE students
• SET age = 21
• WHERE student_id = 1;
• DELETE: Removes data from a table.
• DELETE FROM students
• WHERE student_id = 1;
d. Data Control Language (DCL)
• GRANT: Gives users permissions (e.g., SELECT, INSERT, etc.).
• GRANT SELECT ON students TO user1;
• REVOKE: Removes permissions.
• REVOKE SELECT ON students FROM user1;
3. Basic SQL Queries
• SELECT Specific Columns: To select specific columns (e.g., first name and age):
• SELECT first_name, age FROM students;
• WHERE Clause:
Filters data based on a condition.
• SELECT * FROM students
• WHERE age > 18;
• AND/OR:
Combines multiple conditions.
• SELECT * FROM students
• WHERE age > 18 AND last_name = 'Doe';
• ORDER BY:
Sorts the results in ascending (ASC) or descending (DESC) order.
• SELECT * FROM students
• ORDER BY age DESC;
4. Advanced SQL Queries
• GROUP BY:
Groups rows that have the same values into summary rows.
• SELECT COUNT(*), age FROM students
• GROUP BY age;
• HAVING:
Filters results after a GROUP BY operation.
• SELECT COUNT(*), age FROM students
• GROUP BY age
• HAVING age > 18;
• JOIN:
Combines rows from two or more tables based on a related column.
o INNER JOIN: Returns rows that have matching values in both tables.
o SELECT students.first_name, courses.course_name
o FROM students
o INNER JOIN enrollments ON students.student_id =
enrollments.student_id
o INNER JOIN courses ON enrollments.course_id = courses.course_id;
o LEFT JOIN: Returns all rows from the left table, and the matched rows from the
right table.
o SELECT students.first_name, courses.course_name
o FROM students
o LEFT JOIN enrollments ON students.student_id =
enrollments.student_id
o LEFT JOIN courses ON enrollments.course_id = courses.course_id;
• DISTINCT:
Removes duplicate values.
• SELECT DISTINCT age FROM students;
5. SQL Functions
• COUNT():
Returns the number of rows.
• SELECT COUNT(*) FROM students;
• AVG():
Returns the average of a column.
• SELECT AVG(age) FROM students;
• SUM():
Returns the sum of a numeric column.
• SELECT SUM(age) FROM students;
• MIN() / MAX():
Returns the minimum or maximum value of a column.
• SELECT MIN(age) FROM students;
• SELECT MAX(age) FROM students;
• CONCAT():
Combines two or more strings.
• SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM students;
6. Normalization
• What is Normalization?
o The process of organizing data to reduce redundancy and dependency by dividing
large tables into smaller ones.
o Aims to ensure that the database is efficient and free of data anomalies.
Normal Forms:
o 1NF (First Normal Form): No repeating groups of data.
o 2NF (Second Normal Form): No partial dependency (all non-key attributes are
fully dependent on the primary key).
o 3NF (Third Normal Form): No transitive dependency (non-key attributes are not
dependent on other non-key attributes).
7. Indexes and Views
• Index:
A special data structure that improves the speed of data retrieval operations.
• CREATE INDEX idx_age ON students (age);
• View:
A virtual table based on the result of a query.
• CREATE VIEW student_view AS
• SELECT first_name, last_name, age FROM students WHERE age > 18;
Key Points to Remember:
• SQL is used to interact with databases.
• SELECT is used to retrieve data, INSERT adds data, UPDATE modifies data, and
DELETE removes data.
• Use WHERE to filter results and ORDER BY to sort data.
• JOIN is used to combine data from multiple tables.
This is a simplified summary of SQL. If you need more in-depth explanations or specific
examples, feel free to ask!