The SELECT statement in MySQL is used to retrieve data from one or more tables in a database. To explain its functionality, let’s use the Northwind database, a commonly used sample database that contains tables representing a small business scenario (e.g., customers, orders, products, etc.).


Basic Syntax of SELECT

SELECT column1, column2, ... 
FROM table_name
WHERE condition;
  • column1, column2, ...: The columns you want to retrieve.
  • table_name: The table from which you want to fetch the data.
  • WHERE condition: Optional. Filters the rows returned by the query.

Example Northwind Table: Customers

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany
ANATRAna TrujilloAna TrujilloMexico CityMexico
BERGSBerglunds snabbköpChristina BLuleåSweden

1. Select All Columns

Retrieve all the columns and rows from the Customers table:

SELECT * FROM Customers;

Result:

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany
ANATRAna TrujilloAna TrujilloMexico CityMexico
BERGSBerglunds snabbköpChristina BLuleåSweden

2. Select Specific Columns

Retrieve only CompanyName and Country:

SELECT CompanyName, Country FROM Customers;

Result:

CompanyNameCountry
Alfreds FutterkisteGermany
Ana TrujilloMexico
Berglunds snabbköpSweden

3. Filter Rows with WHERE

Retrieve customers from Germany:

SELECT * FROM Customers WHERE Country = 'Germany';

Result:

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany

4. Sorting Results with ORDER BY

Retrieve customers and sort them by City in ascending order:

SELECT CompanyName, City FROM Customers ORDER BY City ASC;

Result:

CompanyNameCity
Alfreds FutterkisteBerlin
Berglunds snabbköpLuleå
Ana TrujilloMexico City

5. Limiting Results with LIMIT

Retrieve only the first two customers:

SELECT * FROM Customers LIMIT 2;

Result:

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany
ANATRAna TrujilloAna TrujilloMexico CityMexico

6. Using Wildcards with LIKE

Find customers whose ContactName starts with ‘A’:

SELECT * FROM Customers WHERE ContactName LIKE 'A%';

Result:

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany
ANATRAna TrujilloAna TrujilloMexico CityMexico

7. Using Aggregate Functions

Count the total number of customers:

SELECT COUNT(*) AS TotalCustomers FROM Customers;

Result:

TotalCustomers
3

8. Grouping Results with GROUP BY

Group customers by Country and count the number of customers in each country:

SELECT Country, COUNT(*) AS CustomerCount 
FROM Customers 
GROUP BY Country;

Result:

CountryCustomerCount
Germany1
Mexico1
Sweden1

9. Joining Tables

Retrieve the names of customers along with their orders from the Orders and Customers tables:

Customers Table:

CustomerIDCompanyNameContactNameCountry
ALFKIAlfreds FutterkisteMaria AndersGermany

Orders Table:

OrderIDCustomerIDOrderDate
10248ALFKI2023-01-10
SELECT Customers.CompanyName, Orders.OrderDate 
FROM Customers 
JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Result:

CompanyNameOrderDate
Alfreds Futterkiste2023-01-10

10. Subqueries

Find customers who have placed an order:

SELECT * 
FROM Customers 
WHERE CustomerID IN (SELECT CustomerID FROM Orders);

Result:

CustomerIDCompanyNameContactNameCountry
ALFKIAlfreds FutterkisteMaria AndersGermany

Conclusion

The SELECT statement is the foundation of working with MySQL databases. Using it with clauses like WHERE, ORDER BY, GROUP BY, and LIMIT allows you to query, filter, and organize data effectively. Mastering these techniques is essential for building powerful and dynamic database-driven applications.

The SELECT statement in MySQL is used to retrieve data from one or more tables in a database. To explain its functionality, let’s use the Northwind database, a commonly used sample database that contains tables representing a small business scenario (e.g., customers, orders, products, etc.).


Basic Syntax of SELECT

SELECT column1, column2, ... 
FROM table_name
WHERE condition;
  • column1, column2, ...: The columns you want to retrieve.
  • table_name: The table from which you want to fetch the data.
  • WHERE condition: Optional. Filters the rows returned by the query.

Example Northwind Table: Customers

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany
ANATRAna TrujilloAna TrujilloMexico CityMexico
BERGSBerglunds snabbköpChristina BLuleåSweden

1. Select All Columns

Retrieve all the columns and rows from the Customers table:

SELECT * FROM Customers;

Result:

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany
ANATRAna TrujilloAna TrujilloMexico CityMexico
BERGSBerglunds snabbköpChristina BLuleåSweden

2. Select Specific Columns

Retrieve only CompanyName and Country:

SELECT CompanyName, Country FROM Customers;

Result:

CompanyNameCountry
Alfreds FutterkisteGermany
Ana TrujilloMexico
Berglunds snabbköpSweden

3. Filter Rows with WHERE

Retrieve customers from Germany:

SELECT * FROM Customers WHERE Country = 'Germany';

Result:

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany

4. Sorting Results with ORDER BY

Retrieve customers and sort them by City in ascending order:

SELECT CompanyName, City FROM Customers ORDER BY City ASC;

Result:

CompanyNameCity
Alfreds FutterkisteBerlin
Berglunds snabbköpLuleå
Ana TrujilloMexico City

5. Limiting Results with LIMIT

Retrieve only the first two customers:

SELECT * FROM Customers LIMIT 2;

Result:

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany
ANATRAna TrujilloAna TrujilloMexico CityMexico

6. Using Wildcards with LIKE

Find customers whose ContactName starts with ‘A’:

SELECT * FROM Customers WHERE ContactName LIKE 'A%';

Result:

CustomerIDCompanyNameContactNameCityCountry
ALFKIAlfreds FutterkisteMaria AndersBerlinGermany
ANATRAna TrujilloAna TrujilloMexico CityMexico

7. Using Aggregate Functions

Count the total number of customers:

SELECT COUNT(*) AS TotalCustomers FROM Customers;

Result:

TotalCustomers
3

8. Grouping Results with GROUP BY

Group customers by Country and count the number of customers in each country:

SELECT Country, COUNT(*) AS CustomerCount 
FROM Customers 
GROUP BY Country;

Result:

CountryCustomerCount
Germany1
Mexico1
Sweden1

9. Joining Tables

Retrieve the names of customers along with their orders from the Orders and Customers tables:

Customers Table:

CustomerIDCompanyNameContactNameCountry
ALFKIAlfreds FutterkisteMaria AndersGermany

Orders Table:

OrderIDCustomerIDOrderDate
10248ALFKI2023-01-10
SELECT Customers.CompanyName, Orders.OrderDate 
FROM Customers 
JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Result:

CompanyNameOrderDate
Alfreds Futterkiste2023-01-10

10. Subqueries

Find customers who have placed an order:

SELECT * 
FROM Customers 
WHERE CustomerID IN (SELECT CustomerID FROM Orders);

Result:

CustomerIDCompanyNameContactNameCountry
ALFKIAlfreds FutterkisteMaria AndersGermany

Conclusion

The SELECT statement is the foundation of working with MySQL databases. Using it with clauses like WHERE, ORDER BY, GROUP BY, and LIMIT allows you to query, filter, and organize data effectively. Mastering these techniques is essential for building powerful and dynamic database-driven applications.