SQL Basics¶
What is SQL?¶
- SQL (Structured Query Language) is used to:
- Store data
- Retrieve data
- Update data
-
Delete data from a database.
-
SQL works with relational databases like:
- MySQL
- PostgreSQL
- SQL Server
- Oracle
- SQLite
What is a Database?¶
- A database is a place where data is stored.
What is a Table?¶
- A table stores data in rows and columns.
- Example: employees table:
| id | name | age | department | salary |
|---|---|---|---|---|
| 1 | Rahul | 25 | IT | 50000 |
| 2 | Anita | 28 | HR | 45000 |
- Row → one record
- Column → one field
Basic SQL Command¶
SELECT Statement * Used to read data from a table.
* means all columnsSelect Specific Columns
WHERE Clause (Filtering Data) Using Conditions AND / OR ORDER BY (Sorting Data) * Used to sort data in ascending (ASC) or descending (DESC) order. * Example: Sort by age (youngest first)sql
SELECT * FROM students
ORDER BY age;
* (Default is ASC)
Oldest first
Sort by name alphabetically LIMIT (Restrict Number of Rows) * Used to limit the output rows. * Get first 5 students Top 3 oldest students ORDER BY + WHERE + LIMIT (Very Important Combo) * Get top 2 highest paid IT employeesSQL Operators (IN, BETWEEN, LIKE) * IN Operator - Used when you want to match multiple values. * Instead of:
* Use: * BETWEEN Operator - Used for a range of values. * Example: * Includes both 40000 & 60000 * LIKE Operator (Pattern Matching) - Used with text.| Pattern | Meaning |
| ------- | ------------------------ |
| `%` | Any number of characters |
| `_` | Exactly one character |
- Names starting with 'A'
- Names ending with 'n'
- Names with 'a' in middle