SQL Fundamentals
1. SELECT Statement
The SELECT statement is used to retrieve data from a database. It allows you to specify which columns you want to retrieve and from which table. The basic syntax is:
SELECT column1, column2, ...
FROM table_name;
Example: To retrieve all customer names and their email addresses from a "Customers" table, you would use:
SELECT Name, Email
FROM Customers;
Analogies: Think of the SELECT statement as asking a librarian for a specific book or a list of books from a catalog.
2. WHERE Clause
The WHERE clause is used to filter records based on specific conditions. It helps in narrowing down the data to only those that meet the specified criteria. The basic syntax is:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example: To retrieve all customers who live in New York from the "Customers" table, you would use:
SELECT Name, Email
FROM Customers
WHERE City = 'New York';
Analogies: The WHERE clause is like applying a filter to a search engine to get only the relevant results.
3. JOIN Operation
The JOIN operation is used to combine rows from two or more tables based on a related column between them. It allows you to query data that is spread across multiple tables. The basic syntax is:
SELECT column1, column2, ...
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;
Example: To retrieve customer names along with their order details from the "Customers" and "Orders" tables, you would use:
SELECT Customers.Name, Orders.OrderID
FROM Customers
JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
Analogies: The JOIN operation is like combining two spreadsheets by matching a common column, such as a customer ID.