0% found this document useful (0 votes)
14 views4 pages

SQL Queries with Real-Life Examples

The document provides SQL examples with real-life use cases, including basic queries, filtering with WHERE, sorting with ORDER BY, and using aggregate functions. It also covers grouping data with GROUP BY and matching records using INNER JOIN. Each example includes SQL code, corresponding tables, and expected output.

Uploaded by

demo76692
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

SQL Queries with Real-Life Examples

The document provides SQL examples with real-life use cases, including basic queries, filtering with WHERE, sorting with ORDER BY, and using aggregate functions. It also covers grouping data with GROUP BY and matching records using INNER JOIN. Each example includes SQL code, corresponding tables, and expected output.

Uploaded by

demo76692
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL with Real Life Examples - With Output

1. SELECT Statement - Basic Query

Use Case: List all customers.

SQL:

SELECT * FROM customers;

Table: customers

| id | name | city |

|----|--------|-----------|

| 1 | Ayesha | Mumbai |

| 2 | Ravi | Pune |

| 3 | Sneha | Bangalore |

Output:

| id | name | city |

|----|--------|-----------|

| 1 | Ayesha | Mumbai |

| 2 | Ravi | Pune |

| 3 | Sneha | Bangalore |

2. WHERE Clause

Use Case: Find customers in Pune.

SQL:

SELECT name FROM customers WHERE city = 'Pune';

Output:

| name |

|------|

| Ravi |
SQL with Real Life Examples - With Output

3. ORDER BY Clause

Use Case: List customers alphabetically.

SQL:

SELECT name FROM customers ORDER BY name ASC;

Output:

| name |

|-------|

| Ayesha|

| Ravi |

| Sneha |

4. Aggregate Functions

Use Case: Find total products and average price.

Table: products

| id | name | price |

|----|-------|-------|

| 1 | Milk | 25 |

| 2 | Bread | 15 |

| 3 | Rice | 50 |

SQL:

SELECT COUNT(*) AS total, AVG(price) AS avg_price FROM products;

Output:

| total | avg_price |

|-------|-----------|

| 3 | 30.00 |
SQL with Real Life Examples - With Output

5. GROUP BY + HAVING

Use Case: Students count per grade.

Table: students

| id | name | grade |

|----|-------|-------|

| 1 | Aamir | A |

| 2 | Sana | B |

| 3 | Arjun | A |

SQL:

SELECT grade, COUNT(*) AS total FROM students GROUP BY grade;

Output:

| grade | total |

|-------|-------|

|A | 2 |

|B | 1 |

6. INNER JOIN

Use Case: Match customers with their orders.

customers Table

| id | name |

|----|-------|

| 1 | Ayesha|

| 2 | Ravi |

orders Table

| id | customer_id | item |

|----|-------------|--------|
SQL with Real Life Examples - With Output

|1 |1 | Laptop |

|2 |2 | Phone |

SQL:

SELECT [Link], [Link]

FROM customers c

JOIN orders o ON [Link] = o.customer_id;

Output:

| name | item |

|-------|--------|

| Ayesha| Laptop |

| Ravi | Phone |

Common questions

Powered by AI

SQL aggregate functions perform calculations on a set of values to return a single value. For example, the function 'SELECT COUNT(*) AS total, AVG(price) AS avg_price FROM products;' computes the total number of products and the average price of the products, returning 3 as total and 30.00 as the average price .

Using SELECT * in queries can lead to inefficient use of resources as it retrieves all columns, increasing data transfer between the database and application. It can also negatively impact maintainability since the data retrieved could change when table schemas are updated, leading to unexpected results if column orders change or irrelevant data is included .

The ORDER BY clause sorts the result-set of a query based on one or more columns in either ascending (ASC) or descending (DESC) order. For instance, 'SELECT name FROM customers ORDER BY name ASC;' arranges customer names in alphabetical order, hence the output is Ayesha, Ravi, Sneha .

The WHERE clause in SQL allows you to filter records that meet a specific condition, which refines the query results to only include data that are relevant. For example, in the statement 'SELECT name FROM customers WHERE city = 'Pune';', only customers living in Pune are selected, which in this instance is Ravi .

SQL queries can be optimized by using indexes for faster retrieval of records, minimizing the use of wildcard characters, ensuring that only necessary columns are queried, avoiding complex joins and subqueries when possible, and ensuring proper use of WHERE clauses to filter data early in the query process .

The GROUP BY clause organizes data into groups based on one or more columns. It is frequently used with aggregate functions to perform calculations on each group independently. For instance, 'SELECT grade, COUNT(*) AS total FROM students GROUP BY grade;' provides the count of students for each grade, resulting in two students for grade A and one for grade B .

The HAVING clause filters groups formed by GROUP BY based on a specified condition, similar to WHERE but applied to aggregated data. For instance, if we want to find groups of grades with more than one student, the HAVING clause can follow the GROUP BY to specify this condition, ensuring that only such groups appear in the results .

The INNER JOIN clause is used to combine rows from two or more tables based on a related column between them. The SQL statement 'SELECT c.name, o.item FROM customers c JOIN orders o ON c.id = o.customer_id;' joins the customers and orders tables to match each customer with their corresponding orders, showing results like Ayesha with Laptop and Ravi with Phone .

To perform a LEFT JOIN in SQL, merge data from two tables based on a related column in each table, ensuring all records from the left table appear in the result. For example, 'SELECT c.name, o.item FROM customers c LEFT JOIN orders o ON c.id = o.customer_id;' would return all customers, including those without matching orders, with NULLs in the item column for unmatched cases .

COUNT(*) counts all rows in a table, whereas COUNT(column_name) counts only the rows where the column contains a non-NULL value. COUNT(*) is used when rows' total number is needed, while COUNT(column_name) is useful when we need to count rows with data present in a specific column .

You might also like