Basic Queries

This tutorial introduces fundamental SQL (Structured Query Language) commands for retrieving data from relational databases. Understanding these basic queries is the first step to effectively interacting with your data.

The `SELECT` Statement

The most common SQL statement is SELECT, used to query the database and retrieve data that matches criteria you specify.

Selecting All Columns

To retrieve all columns and all rows from a table, you can use the asterisk (*) wildcard:

SELECT *
FROM Customers;

This query will return every piece of information about every customer in the Customers table.

Selecting Specific Columns

You can also specify which columns you want to retrieve by listing their names after SELECT, separated by commas:

SELECT FirstName, LastName, Email
FROM Customers;

This query will only return the first name, last name, and email address for each customer.

The `WHERE` Clause

The WHERE clause is used to filter records. It extracts only those records that fulfill a specified condition.

Filtering with `WHERE`

Let's say we only want to find customers from a specific city:

SELECT CustomerID, FirstName, LastName, City
FROM Customers
WHERE City = 'London';

This will return the ID, name, and city for all customers whose City is 'London'.

Using Comparison Operators

The WHERE clause supports various comparison operators:

Example: Numeric Comparison

Find products with a price greater than $50:

SELECT ProductName, Price
FROM Products
WHERE Price > 50.00;

Example: String Pattern Matching (`LIKE`)

Find customers whose last name starts with 'S':

SELECT FirstName, LastName
FROM Customers
WHERE LastName LIKE 'S%';

The % wildcard represents zero, one, or multiple characters.

Example: Multiple Values (`IN`)

Find customers from either 'USA' or 'Canada':

SELECT CompanyName, Country
FROM Customers
WHERE Country IN ('USA', 'Canada');

Logical Operators

You can combine conditions in the WHERE clause using logical operators:

Example: Combining Conditions (`AND`)

Find customers from 'USA' who are also located in 'New York':

SELECT CompanyName, Country, City
FROM Customers
WHERE Country = 'USA' AND City = 'New York';

Example: Combining Conditions (`OR`)

Find customers who are either from 'Germany' or have an order date after '2023-01-01':

SELECT CustomerName, Country, OrderDate
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Country = 'Germany' OR OrderDate > '2023-01-01';

The `ORDER BY` Clause

The ORDER BY clause is used to sort the result set in ascending or descending order. By default, it sorts in ascending order (ASC). To sort in descending order, use the DESC keyword.

Example: Sorting Results

List all customers, ordered alphabetically by their last name:

SELECT FirstName, LastName
FROM Customers
ORDER BY LastName ASC;

To sort in reverse alphabetical order:

SELECT FirstName, LastName
FROM Customers
ORDER BY LastName DESC;

Example: Sorting by Multiple Columns

List customers, sorted first by country, then by city within each country:

SELECT CompanyName, Country, City
FROM Customers
ORDER BY Country ASC, City ASC;

Tip: Case Sensitivity

SQL keywords are generally case-insensitive (e.g., SELECT is the same as select). However, string comparisons might be case-sensitive depending on the database system and its configuration.

Important Note

The table and column names used in these examples (Customers, FirstName, LastName, City, Products, ProductName, Price, Orders, OrderDate) are illustrative. You will need to replace them with the actual names from your database schema.

These basic queries form the foundation for data retrieval. As you progress, you'll learn about more complex operations like joins, aggregations, and subqueries.