SQL Overview
Structured Query Language (SQL) is a standardized programming language that is used to manage and manipulate data held in a relational database. SQL is used to access and manipulate data. It's a declarative programming language, which means you tell the database what you want, but not how to get it.
Core Concepts of SQL
SQL is built around a few fundamental concepts:
- Tables: Data is organized into tables, which consist of rows and columns.
- Columns (Attributes): Represent individual pieces of data within a table. Each column has a specific data type (e.g., INTEGER, VARCHAR, DATE).
- Rows (Records/Tuples): Represent a single entry or item in a table.
- Primary Key: A column or a set of columns that uniquely identifies each row in a table.
- Foreign Key: A column or a set of columns in one table that refers to the primary key in another table, establishing a link between them.
Key SQL Commands (DDL & DML)
Data Definition Language (DDL)
DDL statements are used to define, modify, and delete database objects.
CREATE
: Used to create database objects like tables, indexes, and views.CREATE TABLE Customers ( CustomerID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), Email VARCHAR(100) );
ALTER
: Used to modify the structure of an existing database object.ALTER TABLE Customers ADD PhoneNumber VARCHAR(20);
DROP
: Used to delete a database object.DROP TABLE Customers;
Data Manipulation Language (DML)
DML statements are used to manage data within database objects.
SELECT
: Used to retrieve data from one or more tables. This is the most frequently used SQL command.SELECT FirstName, LastName FROM Customers WHERE CustomerID = 10;
INSERT
: Used to add new records (rows) into a table.INSERT INTO Customers (CustomerID, FirstName, LastName, Email) VALUES (101, 'John', 'Doe', 'john.doe@example.com');
UPDATE
: Used to modify existing records in a table.UPDATE Customers SET Email = 'jane.smith@newdomain.com' WHERE CustomerID = 101;
DELETE
: Used to remove records from a table.DELETE FROM Customers WHERE CustomerID = 101;
A Simple SQL Query Example
Let's say we have a Products
table:
Products (ProductID INT PRIMARY KEY, ProductName VARCHAR(100), Price DECIMAL(10, 2), Stock INT);
To find the names and prices of products that cost more than $50:
SELECT ProductName, Price
FROM Products
WHERE Price > 50.00;
SQL Clauses and Operators
SQL uses various clauses and operators to refine queries:
WHERE
: Filters records based on specified conditions.ORDER BY
: Sorts the result set by one or more columns.GROUP BY
: Groups rows that have the same values in specified columns.HAVING
: Filters groups based on specified conditions (used withGROUP BY
).JOIN
: Combines rows from two or more tables based on a related column. (See SQL Joins for more details).
Common operators include comparison operators (=
, >
, <
, >=
, <=
, <>
or !=
), logical operators (AND
, OR
, NOT
), and pattern matching (LIKE
).
Why Learn SQL?
SQL is a fundamental skill for anyone working with data. It's used extensively in:
- Web Development (backend)
- Data Analysis
- Business Intelligence
- Database Administration
- Software Engineering
Most modern database systems, such as MySQL, PostgreSQL, SQL Server, Oracle, and SQLite, use SQL as their standard query language.
This overview provides a foundational understanding of SQL. For more advanced topics and specific database implementations, please refer to the subsequent sections.