MySQL SELECT

MySQL SELECT

MySQL SELECT Statement

The SELECT statement in MySQL is used to retrieve data from one or more tables in a database. It is one of the most fundamental and widely used SQL commands.


Basic Syntax

SELECT column1, column2, ... FROM table_name WHERE condition;

Key Clauses in SELECT

1. SELECT Specific Columns

Retrieve specific columns from a table:

SELECT column1, column2 FROM table_name;

2. SELECT All Columns

Retrieve all columns using the * wildcard:

SELECT * FROM table_name;

Examples

1. Basic SELECT

Retrieve all data from a customers table:

SELECT * FROM customers;

2. Select Specific Columns

Retrieve the name and email columns:

SELECT name, email FROM customers;

3. Using WHERE Clause

Filter Rows by a Condition

Retrieve customers who live in "New York":

SELECT * FROM customers WHERE city = 'New York';

Using Logical Operators

Retrieve customers in either "New York" or "Los Angeles":

SELECT * FROM customers WHERE city = 'New York' OR city = 'Los Angeles';

Using Comparison Operators

Retrieve customers whose age is greater than 30:

SELECT * FROM customers WHERE age > 30;

4. Using ORDER BY

Sort in Ascending Order

Retrieve customers and sort them by name:

SELECT * FROM customers ORDER BY name ASC;

Sort in Descending Order

SELECT * FROM customers ORDER BY name DESC;

5. Using LIMIT

Retrieve only the first 5 rows:

SELECT * FROM customers LIMIT 5;

6. Using DISTINCT

Retrieve unique values from a column:

SELECT DISTINCT city FROM customers;

7. Using Aliases

Column Alias

Rename a column in the output:

SELECT name AS customer_name, email AS contact_email FROM customers;

Table Alias

Rename a table for easier reference:

SELECT c.name, c.email FROM customers AS c;

8. Using JOIN

Inner Join

Retrieve orders along with customer names:

SELECT orders.id, customers.name FROM orders INNER JOIN customers ON orders.customer_id = customers.id;

Left Join

Retrieve all customers, even those without orders:

SELECT customers.name, orders.id FROM customers LEFT JOIN orders ON customers.id = orders.customer_id;

9. Using Aggregate Functions

Retrieve the total number of customers:

SELECT COUNT(*) AS total_customers FROM customers;

Retrieve the average age of customers:

SELECT AVG(age) AS average_age FROM customers;

10. Using GROUP BY

Group data and calculate totals:

SELECT city, COUNT(*) AS total_customers FROM customers GROUP BY city;

11. Using HAVING Clause

Filter grouped data:

SELECT city, COUNT(*) AS total_customers FROM customers GROUP BY city HAVING total_customers > 10;

Practical Examples

Search for Specific Data

Find all customers whose name starts with "A":

SELECT * FROM customers WHERE name LIKE 'A%';

Retrieve Data in a Date Range

Find all orders placed in the last month:

SELECT * FROM orders WHERE order_date BETWEEN '2024-12-01' AND '2024-12-31';

Combining Clauses

Retrieve customers older than 30, sorted by age, and limit the results to 10:

SELECT * FROM customers WHERE age > 30 ORDER BY age DESC LIMIT 10;

Best Practices

  1. Avoid SELECT *: Always specify columns for better performance and clarity.
  2. Use Aliases: Make queries more readable.
  3. Use Indexing: Optimize queries by indexing frequently searched columns.
  4. Test Queries: Use a smaller dataset for testing complex queries.

Let me know if you need help with advanced examples or troubleshooting!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close