0% found this document useful (0 votes)
7 views3 pages

SQL

The document provides an overview of SQL concepts and functions, including AUTO_INCREMENT, aggregate functions like AVG, and transaction commands such as BEGIN TRANSACTION. It also includes examples of SQL queries using window functions like ROW_NUMBER(), RANK(), and DENSE_RANK() to analyze employee salaries, customer purchases, and product sales. Additionally, it explains the ACID properties of database transactions and provides practical use cases for various SQL functions.

Uploaded by

chdeepak96
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)
7 views3 pages

SQL

The document provides an overview of SQL concepts and functions, including AUTO_INCREMENT, aggregate functions like AVG, and transaction commands such as BEGIN TRANSACTION. It also includes examples of SQL queries using window functions like ROW_NUMBER(), RANK(), and DENSE_RANK() to analyze employee salaries, customer purchases, and product sales. Additionally, it explains the ACID properties of database transactions and provides practical use cases for various SQL functions.

Uploaded by

chdeepak96
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/ 3

AUTO_INCREMENT

Attribute used to automatically generate a unique value for a column, typically


used with integer data types.
AUTO_INCREMENT
ramchandrapadwal
AVG
Aggregate function that calculates the average value of a numeric
column in a SELECT statement.
BEGIN TRANSACTION
BETWEEN
SQL command used to start a new transaction.
Operator used in a WHERE clause to retrieve rows with a value within a
specified range, inclusive.
B
ACID PROPERTIES
A set of properties ( Atomicity, Consistency, Isolation, Durability ) that
guarantee the reliability of database transaction
ALIAS
A temporary name given to a table or column in a query for readability
and convenience.
BEGIN TRANSACTION
SQL command used to start a new transaction.
BETWEEN
SQL command used to start a new transaction.
Operator used in a WHERE clause to retrieve rows with a value within a
specified range, inclusive.

1) How would you write a query to find the nth (or 2nd) highest salary based on the department and show only
one employee for each department.

EMPLOYEE_ID | EMPLOYEE_NAME | DEPARTMENT_NAME | SALASRY

Ans.

SELECT * FROM

FROM (SELECT *, ROW_NUMBER()

OVER( PARTITION BY DEPARTMENT_NAME ORDER BY SALARY DESC) AS rn FROM TABLE)

Rn = 2

- PARTITION BY (Groups) divides result into partitions based on DEPARTMENT_ID


- PARTITION BY is used in window functions to divide the result set into partitions (groups) and perform
calculations (like ROW_NUMBER(), RANK(), SUM(), AVG(), etc.) on each partition independently

SELECT column_name,

RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank_in_dept

FROM employees;

TBC-Running total – cumulative total


Function Description Typical Use Case

ROW_NUMBER() Unique row number in each partition Deduplication, pagination


RANK() Rank with gaps for ties Top-N queries
DENSE_RANK() Rank without gaps Similar to RANK but with contiguous values
NTILE(n) Divides rows in partition into n buckets Quartiles, deciles
SUM() Running total or group total Cumulative totals
AVG() Running average or group average Trend analysis
LAG() Value from previous row Compare current row with previous
LEAD() Value from next row Compare current row with next
FIRST_VALUE() First value in the partition Anchor comparisons
LAST_VALUE() Last value in the partition Used with window frame tweaking

Q1.0. Find the first purchase of each customer.

SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY purchase_date) AS rn
FROM purchases
)t
WHERE rn = 1;

Q1.1: Find the 2nd highest salary in each department.


SELECT *
FROM (
SELECT *,
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rk
FROM employees
)t
WHERE rk = 2;

Q2: Get the top 3 selling products in each category by revenue.

SELECT *
FROM (
SELECT *,
RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS rk
FROM products
)t
WHERE rk <= 3;

If two products have the same revenue, RANK() will skip ranks (1, 1, 3). Use DENSE_RANK() if you want sequential
ranks (1, 1, 2).

Q3: Show each employee’s salary and how it compares to the average salary in their department.
SELECT employee_id,
department_id,
salary,
AVG(salary) OVER (PARTITION BY department_id) AS avg_dept_salary,
salary - AVG(salary) OVER (PARTITION BY department_id) AS diff_from_avg
FROM employees;

Q4.0: For each stock symbol, find the change in price from the previous day.
SELECT symbol,
trade_date,
price,
price - LAG(price) OVER (PARTITION BY symbol ORDER BY trade_date) AS price_change
FROM stocks;

Q4.1: Detect first time a user logs in after 7 days of inactivity.


SELECT user_id,
login_date,
LAG(login_date) OVER (PARTITION BY user_id ORDER BY login_date) AS prev_login,
CASE
WHEN DATEDIFF(login_date, LAG(login_date) OVER (PARTITION BY user_id ORDER BY login_date)) > 7
THEN 'Reactivation'
END AS event_type
FROM logins;

Q5: Show each customer's order along with the first product they ever purchased.
SELECT customer_id,
order_id,
product_id,
FIRST_VALUE(product_id) OVER (PARTITION BY customer_id ORDER BY order_date) AS first_product
FROM orders;
FIRST_VALUE() / LAST_VALUE()

You might also like