0% found this document useful (0 votes)
4 views

UNIT-3

The document outlines various SQL functions and concepts, including date and time functions, string functions, and the differences between UNION, INTERSECT, and EXCEPT operators. It explains self-joins, correlated subqueries, the EXISTS clause, and the significance of window functions compared to aggregate functions. Additionally, it discusses the role of subqueries in data analysis and different types of joins for combining data from multiple tables.

Uploaded by

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

UNIT-3

The document outlines various SQL functions and concepts, including date and time functions, string functions, and the differences between UNION, INTERSECT, and EXCEPT operators. It explains self-joins, correlated subqueries, the EXISTS clause, and the significance of window functions compared to aggregate functions. Additionally, it discusses the role of subqueries in data analysis and different types of joins for combining data from multiple tables.

Uploaded by

Farhan Shakeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. How are date and time functions used in SQL?

Date and time functions in SQL are used to manipulate and retrieve date/time values.
Examples include:
o GETDATE() (returns the current date and time)
o DATEADD(interval, value, date) (adds a specific interval to a date)
o DATEDIFF(interval, start_date, end_date) (calculates the difference
between two dates)
o FORMAT(date, format) (formats a date in a specific style)
2. Define string functions and provide examples.
String functions in SQL are used to manipulate text data. Examples:
o UPPER('hello') → returns 'HELLO'
o LOWER('WORLD') → returns 'world'
o LEN('hello') → returns 5 (length of the string)
o SUBSTRING('database', 2, 4) → returns 'atab'
o CONCAT('SQL', ' Server') → returns 'SQL Server'
3. What is the difference between UNION, INTERSECT, and EXCEPT operators?
o UNION combines results from two queries and removes duplicates.
o INTERSECT returns only the common records between two queries.
o EXCEPT returns records from the first query that are not in the second query.
4. Explain the concept of self-joins in SQL.
A self-join occurs when a table is joined with itself. It is useful when comparing rows
within the same table. Example:

SELECT e1.EmployeeID, e1.Name, e2.Name AS Manager


FROM Employees e1
JOIN Employees e2 ON e1.ManagerID = e2.EmployeeID;

This retrieves employees along with their managers.

5. How do correlated subqueries differ from regular subqueries?


o A regular subquery runs independently and returns a result before the outer
query executes.
o A correlated subquery depends on the outer query and runs once for each row in
the outer query. Example:

SELECT EmployeeID, Name, Salary


FROM Employees e1
WHERE Salary > (SELECT AVG(Salary) FROM Employees e2 WHERE
e1.DepartmentID = e2.DepartmentID);

This finds employees earning more than the average salary in their department.

6. What is the significance of the EXISTS clause in SQL?


The EXISTS clause is used to check if a subquery returns any results. It improves
performance in certain cases. Example:

SELECT Name FROM Customers c


WHERE EXISTS (SELECT 1 FROM Orders o WHERE c.CustomerID = o.CustomerID);
This retrieves customers who have placed at least one order.

7. How do window functions differ from aggregate functions?


o Aggregate functions (e.g., SUM, AVG, COUNT) group multiple rows into a single
value.
o Window functions compute results across a defined set of rows while retaining
individual row details. Example:

SELECT Name, Salary,


AVG(Salary) OVER (PARTITION BY DepartmentID) AS AvgDeptSalary
FROM Employees;

This calculates the average salary per department without collapsing rows.

8. Explain the use of numeric functions like SQRT and ROUND.


o SQRT(value): Returns the square root of a number. Example: SQRT(25) → 5.0
o ROUND(value, decimal_places): Rounds a number to a specific decimal place.
Example: ROUND(123.456, 2) → 123.46
9. Describe how joins can be used for data exploration.
Joins help in exploring relationships between tables. For example:
o INNER JOIN helps find related records (e.g., orders and customers).
o LEFT JOIN finds missing relationships (e.g., customers with or without orders).
o FULL JOIN shows all records from both tables.
10. What is the significance of the CROSS JOIN operation?
A CROSS JOIN creates a Cartesian product of two tables, meaning every row from the
first table is combined with every row from the second. Example:
SELECT * FROM Products CROSS JOIN Categories;

1. Role of Subqueries in Data Analysis

A subquery is a query nested inside another query. It is useful in data analysis for:

 Filtering results dynamically


 Performing aggregations before the main query
 Using results of one query in another

Example: Finding employees with salaries above the company average

SELECT name, salary

FROM employees

WHERE salary > (SELECT AVG(salary) FROM employees);


2. SQL for Exploratory Data Analysis (EDA)

SQL helps analyze datasets through:

 Summary statistics using COUNT(), AVG(), SUM(), etc.


 Identifying missing values with NULL checks
 Finding distributions using GROUP BY and ORDER BY

Example: Getting basic statistics for a sales table

SELECT

COUNT(*) AS total_sales,

AVG(amount) AS avg_sale,

MIN(amount) AS min_sale,

MAX(amount) AS max_sale

FROM sales;

3. EXISTS Operator

The EXISTS operator checks if a subquery returns any rows. It’s commonly used for existence
checks in related tables.

Example: Finding customers who have made at least one order

SELECT name

FROM customers c

WHERE EXISTS (

SELECT 1 FROM orders o

WHERE o.customer_id = c.customer_id

);

4. Different Types of Joins

Joins help combine data from multiple tables.

1. INNER JOIN (Only matching records in both tables)

SELECT employees.name, departments.department_name


FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
2. LEFT JOIN (All from left table + matching from right)

SELECT employees.name, departments.department_name


FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;

Employees without a department will still appear.

3. RIGHT JOIN (All from right table + matching from left)

SELECT employees.name, departments.department_name


FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;

Departments without employees will still appear.

4. FULL JOIN (All records from both tables)

SELECT employees.name, departments.department_name


FROM employees
FULL JOIN departments ON employees.department_id = departments.department_id;

This retrieves all employees and all departments, even if there’s no match.

5. Window Functions for Ranking & Aggregation

Window functions allow aggregations without collapsing rows, useful for rankings, running
totals, etc.

Example: Ranking Employees by Salary

SELECT name, salary,


RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;

This assigns a ranking based on salary.

Example: Running Total of Sales

SELECT order_id, amount,


SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM sales;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy