Initializing Core...
Initializing Core...
Operational
This guide is your step between the Database Introduction and solving real SQL problems.
Weβll cover the fundamentals you need before jumping into more advanced queries β concepts that will save you from common beginner mistakes and help you write efficient, reliable SQL.
Before running queries, itβs important to understand:
A table stores related data in rows (records) and columns (fields).
Example:
| id | name | age | |
|---|---|---|---|
| 1 | Alice | alice@mail.com | 25 |
| 2 | Bob | bob@mail.com | 30 |
:::tip Tip Think of a table like a spreadsheet, but with strict rules to keep data consistent. :::
Example:
user_id in the Orders table can link to id in the Users table.
An index is like a table of contents for your data β it helps the database find information faster.
But be careful: indexes speed up reads but can slow down inserts and updates.
| Type | Example | Use Case |
|---|---|---|
| INT | 42 | IDs, counters |
| VARCHAR | 'Alice' | Names, emails |
| DATE | '2025-08-14' | Birthdays, deadlines |
| BOOLEAN | TRUE / FALSE | Status flags |
:::tip Did You Know?
NULL is not equal to 0 or an empty string β it means βunknownβ.
:::
SQL (Structured Query Language) is the standard way to interact with relational databases.
Itβs split into categories based on what youβre trying to do.
CREATE, ALTER, DROPINSERT, UPDATE, DELETESELECTGRANT, REVOKECOMMIT, ROLLBACKCREATE TABLE Students (id INT PRIMARY KEY,name VARCHAR(50),age INT);
INSERT INTO Students (id, name, age)VALUES (1, 'Alice', 22);
SELECT name, age FROM Students;
UPDATE StudentsSET age = 23WHERE id = 1;
DELETE FROM StudentsWHERE id = 1;
:::tip Tip
Always include a WHERE clause when updating or deleting data β unless you really want to change or remove every record in the table.
:::
Filtering and sorting help you narrow down results and present them in the desired order. This is essential when working with large datasets.
WHEREThe WHERE clause specifies conditions for selecting rows.
You can use comparison operators and logical operators inside it.
Example:
SELECT *FROM employeesWHERE department = 'Sales';
:::tip Tip
Always double-check your WHERE clause in a SELECT query before using it in UPDATE or DELETE.
:::
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | WHERE age = 30 |
!= or <> | Not equal to | WHERE age != 30 |
< | Less than | WHERE age < 30 |
> | Greater than | WHERE age > 30 |
<= | Less than or equal | WHERE age <= 30 |
>= | Greater than or equal | WHERE age >= 30 |
| Operator | Meaning |
|---|---|
AND | All conditions must be true |
OR | At least one condition must be true |
NOT | Negates a condition |
Example:
SELECT name, salaryFROM employeesWHERE department = 'IT'AND salary > 50000;
| Symbol | Meaning |
|---|---|
% | Matches zero or more characters |
_ | Matches exactly one character |
Example:
SELECT *FROM customersWHERE name LIKE 'A%';-- Finds names starting with 'A'
Sorts results in ascending (ASC) or descending (DESC) order.
Default order is ascending.
Example:
SELECT *FROM productsORDER BY price DESC;
What it covers:
INNER, LEFT, RIGHT joins with examples using two tables.COUNT, SUM, AVG, MIN, MAX along with GROUP BY and HAVING for summarizing and filtering grouped results.:::tip Tip
When using GROUP BY, every non-aggregated column in the SELECT clause must appear in the GROUP BY.
:::
Join Example:
SELECT o.order_id, c.customer_nameFROM Orders oJOIN Customers c ON o.customer_id = c.customer_id;
What it does:
This query uses an INNER JOIN to fetch matching rows from the Orders table (o) and the Customers table (c), based on the customer_id column.
Result:
You get a list of order IDs with their corresponding customer names. Only orders that have a matching customer in the Customers table will be shown.
Aggregation Example:
SELECT customer_id, COUNT(order_id) AS total_ordersFROM OrdersGROUP BY customer_idHAVING COUNT(order_id) > 5;
What it does:
GROUP BY customer_id β Groups orders by each customer.COUNT(order_id) β Counts how many orders each customer placed.HAVING COUNT(order_id) > 5 β Filters to only include customers who placed more than 5 orders.Result:
You get a list of customers (by ID) along with the number of orders they placed, but only for those with more than 5 orders.
:::tip π‘ Tip
Always remember β in SQL, HAVING filters after grouping, while WHERE filters before grouping.
:::
| Constraint | Description |
|---|---|
NOT NULL | Ensures a column cannot have NULL values. |
UNIQUE | Prevents duplicate values in a column. |
DEFAULT | Assigns a default value if none is provided. |
CHECK | Enforces a condition on column values. |
Example:
CREATE TABLE Employees (id INT PRIMARY KEY,name VARCHAR(50) NOT NULL,email VARCHAR(100) UNIQUE,salary DECIMAL(10, 2) DEFAULT 30000,age INT CHECK (age >= 18));
This section introduces core database concepts, covering relational database basics, SQL fundamentals, and how to work with data effectively. It explains essential commands for creating, reading, updating, and deleting records (CRUD), demonstrates filtering and sorting with WHERE, ORDER BY, and LIMIT, and shows how to retrieve related data using joins. Aggregation and grouping with GROUP BY and HAVING enable meaningful data analysis, while constraints like NOT NULL, UNIQUE, DEFAULT, and CHECK ensure data integrity and reliability.