Introduction to SQL
SQL (Structured Query Language) is the standard language used to interact with databases. It allows you to store, retrieve, update, and delete data in a database. SQL is essential for working with relational databases like MySQL, PostgreSQL, Oracle, and SQL Server.
Whether you're building a website, analyzing data, or developing software, SQL helps you manage and organize information efficiently.
History of SQL
- 1970s: SQL was first developed at IBM by Donald D. Chamberlin and Raymond F. Boyce. They created a language called SEQUEL, which later became SQL.
- 1979: Oracle (then called Relational Software, Inc.) released the first commercial SQL-based database.
- 1986: SQL was adopted as a standard by ANSI (American National Standards Institute).
- 1987: It was also adopted by ISO (International Organization for Standardization).
Since then, SQL has become the foundation for most relational database systems.
Uses of SQL
SQL is used in many fields and for many purposes, including:
- Data Management: Create, read, update, and delete data in databases (often called CRUD operations).
- Data Analysis: Run queries to analyze and summarize data (e.g., finding sales trends).
- Web Development: Store user information, posts, products, etc., in web applications.
- Business Intelligence: Generate reports and dashboards using tools like Power BI or Tableau.
- App Development: Back-end systems use SQL to store and manage app data.
- Data Science: Retrieve and filter large datasets from databases for analysis.
Conclusion
SQL is a powerful and essential tool in today’s digital world. Whether you're a beginner or an experienced developer, learning SQL opens up a wide range of opportunities in data and technology.
Types of SQL Statements
SQL is divided into different types based on what the statements do. Here are the main categories:
1. Data Definition Language (DDL)
These statements define and manage database structure like tables and schemas.
CREATE:
Creates a new table or database.
Example :CREATE TABLE Students (ID INT, Name VARCHAR(50));
ALTER:
Modifies an existing table (e.g., add or remove columns).DROP:
Deletes tables or databases permanently.TRUNCATE:
Removes all records from a table but keeps the structure.
2. Data Manipulation Language (DML)
These statements are used to modify data inside the tables.
INSERT:
Adds new data.
Example :INSERT INTO Students VALUES (1, 'Amit');
UPDATE:
Changes existing data.DELETE:
Removes specific data.
3. Data Query Language (DQL)
Used to retrieve data from the database.
SELECT:
Fetches data from one or more tables.
Example :SELECT * FROM Students;
4. Data Control Language (DCL)
These statements control access permissions to the database.
GRANT:
Gives specific rights to users.REVOKE:
Removes those rights.
5. Transaction Control Language (TCL)
These commands handle transactions and ensure data integrity.
COMMIT:
Saves all changes made in the transaction.ROLLBACK:
Undoes changes made if something goes wrong.SAVEPOINT:
Sets a point to which you can rollback later.
Summary Table
Category | Purpose | Common Commands |
---|---|---|
DDL | Define structure | CREATE, ALTER, DROP,
TRUNCATE |
DML | Modify data | INSERT, UPDATE, DELETE |
DQL | Query data | SELECT |
DCL | Control access | GRANT, REVOKE |
TCL | Manage transactions | COMMIT, ROLLBACK,
SAVEPOINT |
What is RDBMS?
RDBMS stands for Relational Database Management System. It is a type of database management system that stores data in the form of tables (rows and columns). Each table is called a relation, and the structure helps keep data organized, connected, and easy to retrieve.
RDBMS follows relational model rules, where:
- Each row is a unique record.
- Each column represents a field or attribute.
- Keys (like primary and foreign keys) are used to link tables together.
Popular RDBMS software includes: MySQL, PostgreSQL, Oracle, SQL Server, and SQLite.
Example of RDBMS
Let’s look at two related tables in a school database:
Table 1: Students
StudentID | Name | Age |
---|---|---|
1 | Aman | 14 |
2 | Priya | 15 |
3 | Ravi | 14 |
Table 2: Marks
MarkID | StudentID | Subject | Score |
---|---|---|---|
1 | 1 | Math | 89 |
2 | 2 | Science | 92 |
3 | 1 | English | 85 |
- The StudentID in the Marks table is a foreign key that refers to the StudentID in the Students table.
- This relationship connects the data, so we know which marks belong to which student.
Why Use RDBMS?
- Data is organized and easy to manage.
- Relationships between tables reduce data repetition.
- Queries can fetch related data from multiple tables using JOINs.
- Ensures data integrity and consistency.