Introduction to SQL
SQL, or Structured Query Language, is a standardized language used for managing and manipulating data stored in relational database management systems (RDBMS). This documentation provides an overview of fundamental SQL concepts and syntax.
Key features of SQL include:
- Data Definition: Creating and modifying database structures.
- Data Manipulation: Inserting, updating, and deleting data.
- Data Control: Managing access permissions and security.
Basic Queries
Let's start with some fundamental queries.
SELECT * FROM Customers; -- Retrieve all data from the Customers table.
SELECT FirstName, LastName FROM Customers WHERE Country = 'USA'; -- Filter customers from the USA.
INSERT INTO Products (ProductName, Price) VALUES ('Laptop', 1200.00); -- Add a new product.
UPDATE Orders SET Status = 'Shipped' WHERE OrderID = 123;
DELETE FROM Customers WHERE CustomerID = 456;
JOIN Operations
Joining tables allows you to combine data from multiple tables based on related columns.
SELECT o.OrderID, c.FirstName, c.LastName
FROM Orders o
JOIN Customers c ON o.CustomerID = c.CustomerID;
Aggregate Functions
Aggregate functions perform calculations on a set of rows.
SELECT COUNT(*) FROM Orders; -- Count the total number of orders.
SELECT AVG(Price) FROM Products; -- Calculate the average price of products.
SELECT MAX(OrderDate) FROM Orders; -- Find the latest order date.