Please Help Me With Real Time SQL Query For ETL T...
Please Help Me With Real Time SQL Query For ETL T...
SELECT
order_id, customer_id, order_date, total_amount, status
FROM
orders
WHERE
status NOT IN ('Pending', 'Shipped', 'Delivered');
● Scenario: You have a customer table with customer_id and country and an orders table.
Requirement: Calculate the total order amount for each country.
SELECT
c.country, SUM(o.total_amount) AS total_order_amount
FROM
customers c
JOIN
orders o ON c.customer_id = o.customer_id
GROUP BY
c.country;
SELECT
date,
sales - LAG(sales) OVER (ORDER BY date) AS daily_sales_difference
FROM
sales_table;
SELECT
customer_id, COUNT(*)
FROM
customers
GROUP BY
customer_id
HAVING
COUNT(*) > 1;
● Scenario: You have a table with an email column.
Requirement:Qquery to identify invalid email addresses (e.g., missing '@' symbol).
SELECT
email
FROM
customers
WHERE
email NOT LIKE '%@%';
SELECT
*
FROM
customers
WHERE
date_of_birth > CURRENT_DATE;
SELECT
oi.product_id, COUNT(DISTINCT o.order_id) AS total_orders
FROM
orders o
JOIN
order_items oi ON o.order_id = oi.order_id
GROUP BY
oi.product_id;
SELECT
customer_id, MIN(order_date) AS first_order_date
FROM
orders
GROUP BY
customer_id;
4. Window Functions
● Scenario: You have a table with order_id, customer_id, and order_date.
Requirement: Query to find the most recent order date for each customer.
SELECT
customer_id, order_date
FROM
(
SELECT
customer_id, order_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY
order_date DESC) AS rn
FROM
orders
) ranked_orders
WHERE
rn = 1;
Remember to adapt these examples to the specific requirements and data structures
presented in the interview.
I hope this helps! Let me know if you have any other questions.