SQL Fundamentals (Pre-requisite)
SQL (Structured Query Language) is the standard language for interacting with relational databases. It allows users to perform various operations such as querying, updating, and managing data within a database.
Here’s a breakdown of key SQL fundamentals with examples:
1. SQL Commands
SQL is categorized into several types of commands:
Data Query Language (DQL):
- Used for querying data from a database.
- Command:
SELECT
Data Definition Language (DDL):
- Used to define or modify database schema.
- Commands:
CREATE
,ALTER
,DROP
,TRUNCATE
Data Manipulation Language (DML):
- Used for manipulating data.
- Commands:
INSERT
,UPDATE
,DELETE
Data Control Language (DCL):
- Used to control access to data.
- Commands:
GRANT
,REVOKE
Transaction Control Language (TCL):
- Used to manage database transactions.
- Commands:
COMMIT
,ROLLBACK
,SAVEPOINT
2. SELECT Statement (DQL)
Example 1: Retrieve All Data from a Table
Explanation:
*
selects all columns from theemployees
table.
Example 2: Retrieve Specific Columns
Explanation:
- Retrieves only the
first_name
andlast_name
columns from theemployees
table.
Example 3: Using WHERE Clause for Filtering
Explanation:
- Retrieves employees with a salary greater than 50,000.
Example 4: Sorting Results with ORDER BY
Explanation:
- Sorts employees by
salary
in descending order.
Example 5: Aggregating Data with GROUP BY
Explanation:
- Calculates the average salary for each department.
3. INSERT Statement (DML)
Example: Insert a Single Row
Explanation:
- Inserts a new employee record into the
employees
table.
4. UPDATE Statement (DML)
Example: Update Data in a Table
Explanation:
- Increases the salary of employees in department 10 by 10%.
5. DELETE Statement (DML)
Example: Delete Specific Records
Explanation:
- Deletes all employees in department 5.
6. CREATE TABLE Statement (DDL)
Example: Create a New Table
Explanation:
- Creates a table named
employees
with specified columns and a primary key.
7. ALTER TABLE Statement (DDL)
Example: Add a New Column
Explanation:
- Adds a
hire_date
column to theemployees
table.
8. DROP TABLE Statement (DDL)
Example: Delete a Table
Explanation:
- Deletes the
employees
table and all its data.
9. JOINs
Example: INNER JOIN
Explanation:
- Combines
employees
anddepartments
tables based ondepartment_id
.
Example: LEFT JOIN
Explanation:
- Retrieves all employees, including those without a department.
10. Transactions (TCL)
Example: COMMIT and ROLLBACK
Explanation:
- Demonstrates transaction control with
ROLLBACK
andCOMMIT
.
11. Subqueries
Example: Subquery in WHERE Clause
Explanation:
- Retrieves employees whose salary is above the average salary.
12. Functions
Example: Using SQL Functions
Explanation:
UPPER
: Converts text to uppercase.LENGTH
: Returns the length of a string.
Conclusion
Understanding SQL fundamentals is essential for working with PL/SQL and databases effectively. Start with basic commands, practice frequently, and progressively move towards more complex queries and concepts. SQL serves as the backbone for data manipulation and is integral to database programming.
Leave a Comment