Below are some clear and understandable notes for beginners to SQL (Structured Query Language):
Introduction to SQL:
SQL (Structured Query Language) is a programming language used to manage and manipulate relational databases.
It allows you to perform various operations on data, such as querying, inserting, updating, and deleting records in a database.
Basic SQL Commands:
SELECT: Used to retrieve data from one or more database tables.
SELECT column1, column2 FROM table_name;
2. INSERT: Used to add new records into a table.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
3.UPDATE: Used to modify existing records in a table.
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
4. DELETE: Used to remove records from a table.
DELETE FROM table_name WHERE condition;
Creating a Table:
To create a new table, use the CREATE TABLE statement with column names and data types
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Constraints:
Constraints are rules applied to columns to maintain data integrity. Some common constraints are:
NOT NULL: Ensures a column cannot have NULL values.
UNIQUE: Ensures each value in a column is unique.
PRIMARY KEY: A combination of NOT NULL and UNIQUE, used to identify a unique row in a table.
FOREIGN KEY: Establishes a link between two tables, referencing a primary key in another table.
Filtering Data:
You can use the WHERE clause to filter data based on certain conditions in a SELECT statement.
SELECT column1, column2 FROM table_name WHERE condition;
Sorting Data:
To sort the results of a SELECT statement, use the ORDER BY clause.
SELECT column1, column2 FROM table_name ORDER BY column1 ASC/DESC;
Joins:
Joins are used to combine data from multiple tables based on a related column. Common types of joins are:
INNER JOIN: Returns only the matching rows from both tables.
LEFT JOIN (or LEFT OUTER JOIN): Returns all the rows from the left table and matching rows from the right table.
RIGHT JOIN (or RIGHT OUTER JOIN): Returns all the rows from the right table and matching rows from the left table.
FULL JOIN (or FULL OUTER JOIN): Returns all the rows when there is a match in either the left or right table.
Grouping Data:
The GROUP BY clause is used to group rows that have the same values in one or more columns.
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
Aggregate Functions:
Aggregate functions perform calculations on grouped data. Common aggregate functions are:
COUNT: Returns the number of rows.
SUM: Returns the sum of values in a column.
AVG: Returns the average of values in a column.
MAX: Returns the maximum value in a column.
MIN: Returns the minimum value in a column.
Updating Multiple Records:
You can update multiple records at once using the WHERE clause in the UPDATE statement.
UPDATE table_name SET column1 = value1 WHERE condition;
Deleting Multiple Records:
You can delete multiple records at once using the WHERE clause in the DELETE statement.
DELETE FROM table_name WHERE condition;