SQL NOTES
SQL NOTES
What is SQL?
SQL (Structured Query Language) is a programming language used for managing and
manipulating data in relational databases. Think of it as the way you talk to a database to get the
information you need.
Key Concepts:
* Relational Databases: Databases organized into tables with rows (records) and columns
(fields).
* Tables: Collections of related data. Like a spreadsheet, but more structured.
* Fields (Columns): Represent specific attributes of the data (e.g., Name, Age, City).
* Records (Rows): Represent a single instance of the data (e.g., one student's information).
* Primary Key: A unique identifier for each record in a table (e.g., StudentID).
* Foreign Key: A field in one table that refers to the primary key in another table, used to link
tables together.
Basic SQL Commands:
SELECT: Used to retrieve data from a database.
SELECT column1, column2, ... FROM table_name; -- Selects specific columns
SELECT * FROM table_name;-- Selects all columns
Example: INSERT INTO Students (Name, Age) VALUES ('Jane Doe', 16);
* UPDATE: Used to modify existing records in a table.
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(255),
Age INT
);
* ALTER TABLE: Used to modify the structure of an existing table (e.g., add or remove
columns).
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE table_name DROP COLUMN column_name;
* DROP TABLE: Used to delete an entire table from the database.
DROP TABLE table_name;
Data Types:
Common data types you'll encounter:
* INT: Integer numbers.
* VARCHAR(n): Text strings of variable length (up to n characters).
* DATE: Dates (YYYY-MM-DD).
* BOOLEAN: True/False values.
Operators:
* =: Equal to
* >: Greater than
* <: Less than
* >=: Greater than or equal to
* <=: Less than or equal to
* != or <>: Not equal to
* LIKE: Used for pattern matching (e.g., WHERE Name LIKE 'J%'; finds names starting with
'J').
* AND, OR, NOT: Logical operators to combine conditions.
Important Notes:
* SQL commands are generally not case-sensitive (e.g., SELECT is the same as select), but it's
good practice to use uppercase for commands and lowercase for table and column names for
readability.
* Semicolons (;) are used to separate SQL statements.
* Practice is key to learning SQL. Try writing and running simple queries on a sample database.
This provides a good starting point for Grade 9 IT students learning SQL. As they progress, they
can learn more advanced topics like joins, subqueries, indexes, and database design principles.