Top 10 Advanced SQL Queries for Data Mastery
1. Recursive CTE (Common Table Expressions)
Use a recursive CTE to traverse hierarchical data, such as employees and their managers.
2. Pivoting Data
Turn row data into columns (e.g., show product categories as separate columns).
3. Window Functions
Calculate a running total of sales based on order date.
4. Ranking with Window Functions
Rank employeesโ salaries within each department.
5. Finding Gaps in Sequences
Identify missing values in a sequential dataset (e.g., order numbers).
6. Unpivoting Data
Convert columns into rows to simplify analysis of multiple attributes.
7. Finding Consecutive Events
Check for consecutive days/orders for the same product using
8. Aggregation with the FILTER Clause
Calculate selective averages (e.g., only for the Sales department).
9. JSON Data Extraction
Extract values from JSON columns directly in SQL.
10. Using Temporary Tables
Create a temporary table for intermediate results, then join it with other tables.
Why These Matter
Advanced SQL queries let you handle complex data manipulation and analysis tasks with ease. From traversing hierarchical relationships to reshaping data (pivot/unpivot) and working with JSON, these techniques expand your ability to derive insights from relational databases.
Keep practicing these queries to solidify your SQL expertise and make more data-driven decisions!
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
#sql #dataanalyst
1. Recursive CTE (Common Table Expressions)
Use a recursive CTE to traverse hierarchical data, such as employees and their managers.
WITH RECURSIVE EmployeeHierarchy AS (
SELECT employee_id, employee_name, manager_id
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.employee_id, e.employee_name, e.manager_id
FROM employees e
JOIN EmployeeHierarchy eh ON e.manager_id = eh.employee_id
)
SELECT *
FROM EmployeeHierarchy;
2. Pivoting Data
Turn row data into columns (e.g., show product categories as separate columns).
SELECT *
FROM (
SELECT TO_CHAR(order_date, 'YYYY-MM') AS month, product_category, sales_amount
FROM sales
) AS pivot_data
PIVOT (
SUM(sales_amount)
FOR product_category IN ('Electronics', 'Clothing', 'Books')
) AS pivoted_sales;
3. Window Functions
Calculate a running total of sales based on order date.
SELECT
order_date,
sales_amount,
SUM(sales_amount) OVER (ORDER BY order_date) AS running_total
FROM sales;
4. Ranking with Window Functions
Rank employeesโ salaries within each department.
SELECT
department,
employee_name,
salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees;
5. Finding Gaps in Sequences
Identify missing values in a sequential dataset (e.g., order numbers).
WITH Sequences AS (
SELECT MIN(order_number) AS start_seq, MAX(order_number) AS end_seq
FROM orders
)
SELECT start_seq + 1 AS missing_sequence
FROM Sequences
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.order_number = Sequences.start_seq + 1
);
6. Unpivoting Data
Convert columns into rows to simplify analysis of multiple attributes.
SELECT
product_id,
attribute_name,
attribute_value
FROM products
UNPIVOT (
attribute_value FOR attribute_name IN (color, size, weight)
) AS unpivoted_data;
7. Finding Consecutive Events
Check for consecutive days/orders for the same product using
LAG().WITH ConsecutiveOrders AS (
SELECT
product_id,
order_date,
LAG(order_date) OVER (PARTITION BY product_id ORDER BY order_date) AS prev_order_date
FROM orders
)
SELECT product_id, order_date, prev_order_date
FROM ConsecutiveOrders
WHERE order_date - prev_order_date = 1;
8. Aggregation with the FILTER Clause
Calculate selective averages (e.g., only for the Sales department).
SELECT
department,
AVG(salary) FILTER (WHERE department = 'Sales') AS avg_salary_sales
FROM employees
GROUP BY department;
9. JSON Data Extraction
Extract values from JSON columns directly in SQL.
SELECT
order_id,
customer_id,
order_details ->> 'product' AS product_name,
CAST(order_details ->> 'quantity' AS INTEGER) AS quantity
FROM orders;
10. Using Temporary Tables
Create a temporary table for intermediate results, then join it with other tables.
-- Create a temporary table
CREATE TEMPORARY TABLE temp_product_sales AS
SELECT product_id, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY product_id;
-- Use the temp table
SELECT p.product_name, t.total_sales
FROM products p
JOIN temp_product_sales t ON p.product_id = t.product_id;
Why These Matter
Advanced SQL queries let you handle complex data manipulation and analysis tasks with ease. From traversing hierarchical relationships to reshaping data (pivot/unpivot) and working with JSON, these techniques expand your ability to derive insights from relational databases.
Keep practicing these queries to solidify your SQL expertise and make more data-driven decisions!
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
#sql #dataanalyst
๐8โค5
SQL (Structured Query Language) is the universal language of databases. Whether you're analyzing sales data, optimizing marketing campaigns, or tracking user behavior, SQL is your go-to tool for:
โ Accessing and managing data efficiently
โ Writing queries to extract insights
โ Building a strong foundation for advanced tools like Python, R, or Power BI
In short, SQL is the bridge between raw data and actionable insights. ๐
SQL Topics to Learn for Data Analyst/Business Analyst Roles
1. Basic:
* SELECT statements
* WHERE clause
* JOINs (INNER, LEFT, RIGHT, FULL)
* GROUP BY and HAVING
* ORDER BY
* Basic Aggregate Functions (COUNT, SUM, AVG, MIN, MAX)
2. Intermediate:
* Subqueries
* CASE statements
* UNION and UNION ALL
* Common Table Expressions (CTEs)
* Window Functions (ROW_NUMBER, RANK, DENSE_RANK, OVER)
* Data Manipulation (INSERT, UPDATE, DELETE)
* Indexes and Performance Tuning
3. Advanced:
* Advanced Window Functions (LEAD, LAG, NTILE)
* Complex Subqueries and Correlated Subqueries
* Advanced Performance Tuning
SQL is not just a skillโitโs the foundation of your data career. ๐
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
โ Accessing and managing data efficiently
โ Writing queries to extract insights
โ Building a strong foundation for advanced tools like Python, R, or Power BI
In short, SQL is the bridge between raw data and actionable insights. ๐
SQL Topics to Learn for Data Analyst/Business Analyst Roles
1. Basic:
* SELECT statements
* WHERE clause
* JOINs (INNER, LEFT, RIGHT, FULL)
* GROUP BY and HAVING
* ORDER BY
* Basic Aggregate Functions (COUNT, SUM, AVG, MIN, MAX)
2. Intermediate:
* Subqueries
* CASE statements
* UNION and UNION ALL
* Common Table Expressions (CTEs)
* Window Functions (ROW_NUMBER, RANK, DENSE_RANK, OVER)
* Data Manipulation (INSERT, UPDATE, DELETE)
* Indexes and Performance Tuning
3. Advanced:
* Advanced Window Functions (LEAD, LAG, NTILE)
* Complex Subqueries and Correlated Subqueries
* Advanced Performance Tuning
SQL is not just a skillโitโs the foundation of your data career. ๐
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
โค5๐4๐1
Preparing for a SQL interview?
Focus on mastering these essential topics:
1. Joins: Get comfortable with inner, left, right, and outer joins.
Knowing when to use what kind of join is important!
2. Window Functions: Understand when to use
ROW_NUMBER, RANK(), DENSE_RANK(), LAG, and LEAD for complex analytical queries.
3. Query Execution Order: Know the sequence from FROM to
ORDER BY. This is crucial for writing efficient, error-free queries.
4. Common Table Expressions (CTEs): Use CTEs to simplify and structure complex queries for better readability.
5. Aggregations & Window Functions: Combine aggregate functions with window functions for in-depth data analysis.
6. Subqueries: Learn how to use subqueries effectively within main SQL statements for complex data manipulations.
7. Handling NULLs: Be adept at managing NULL values to ensure accurate data processing and avoid potential pitfalls.
8. Indexing: Understand how proper indexing can significantly boost query performance.
9. GROUP BY & HAVING: Master grouping data and filtering groups with HAVING to refine your query results.
10. String Manipulation Functions: Get familiar with string functions like CONCAT, SUBSTRING, and REPLACE to handle text data efficiently.
11. Set Operations: Know how to use UNION, INTERSECT, and EXCEPT to combine or compare result sets.
12. Optimizing Queries: Learn techniques to optimize your queries for performance, especially with large datasets.
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
Focus on mastering these essential topics:
1. Joins: Get comfortable with inner, left, right, and outer joins.
Knowing when to use what kind of join is important!
2. Window Functions: Understand when to use
ROW_NUMBER, RANK(), DENSE_RANK(), LAG, and LEAD for complex analytical queries.
3. Query Execution Order: Know the sequence from FROM to
ORDER BY. This is crucial for writing efficient, error-free queries.
4. Common Table Expressions (CTEs): Use CTEs to simplify and structure complex queries for better readability.
5. Aggregations & Window Functions: Combine aggregate functions with window functions for in-depth data analysis.
6. Subqueries: Learn how to use subqueries effectively within main SQL statements for complex data manipulations.
7. Handling NULLs: Be adept at managing NULL values to ensure accurate data processing and avoid potential pitfalls.
8. Indexing: Understand how proper indexing can significantly boost query performance.
9. GROUP BY & HAVING: Master grouping data and filtering groups with HAVING to refine your query results.
10. String Manipulation Functions: Get familiar with string functions like CONCAT, SUBSTRING, and REPLACE to handle text data efficiently.
11. Set Operations: Know how to use UNION, INTERSECT, and EXCEPT to combine or compare result sets.
12. Optimizing Queries: Learn techniques to optimize your queries for performance, especially with large datasets.
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
โค6๐1
Here are 30 most asked SQL questions to clear your next interview -
โค ๐ช๐ถ๐ป๐ฑ๐ผ๐ ๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐
1. Find the cumulative sum of sales for each employee.
2. Rank employees based on their sales within their department.
3. Calculate a running total of orders by order date.
4. Identify the top three salaries in each department.
5. Compute the difference between the current and previous month's sales.
โค ๐๐ผ๐บ๐บ๐ผ๐ป ๐ง๐ฎ๐ฏ๐น๐ฒ ๐๐ ๐ฝ๐ฟ๐ฒ๐๐๐ถ๐ผ๐ป๐ (๐๐ง๐)
1. Write a recursive CTE to generate a sequence of numbers from 1 to 100.
2. Use a CTE to find employees who directly and indirectly report to a specific manager.
3. Calculate the factorial of a number using a recursive CTE.
4. Flatten a hierarchical organization chart using a CTE.
5. Use a CTE to calculate year-over-year growth in sales.
โค ๐๐ผ๐ถ๐ป๐ (๐๐ป๐ป๐ฒ๐ฟ, ๐ข๐๐๐ฒ๐ฟ, ๐๐ฟ๐ผ๐๐, ๐ฆ๐ฒ๐น๐ณ)
1. Retrieve a list of customers who have placed orders (Inner Join).
2. Find employees who have not been assigned to any projects (Left Join).
3. Get a list of projects without assigned employees (Right Join).
4. Generate all possible pairs of products (Cross Join).
5. Match employees to themselves to find pairs from the same department (Self Join).
โค ๐ฆ๐๐ฏ๐พ๐๐ฒ๐ฟ๐ถ๐ฒ๐
1. Find employees earning more than the average salary in their department.
2. Retrieve customers who have placed more than 5 orders.
3. List products that have never been ordered.
4. Identify the second highest salary in the company using a subquery.
5. Find departments where all employees earn above a specific threshold.
โค ๐๐ด๐ด๐ฟ๐ฒ๐ด๐ฎ๐๐ฒ ๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐
1. Calculate the total revenue generated by each product.
2. Find the average salary in each department.
3. Count the number of orders placed by each customer.
4. Find the maximum and minimum sales for each region.
5. Calculate the standard deviation of employee salaries.
โค ๐๐ป๐ฑ๐ฒ๐ ๐ถ๐ป๐ด ๐ฎ๐ป๐ฑ ๐ฃ๐ฒ๐ฟ๐ณ๐ผ๐ฟ๐บ๐ฎ๐ป๐ฐ๐ฒ
1. Identify queries that would benefit from indexing.
2. Compare execution plans with and without an index on a specific column.
3. Check which indexes exist on a table.
4. Optimize a slow query using indexing.
5. Write a query to update a column using an indexed lookup.
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
โค ๐ช๐ถ๐ป๐ฑ๐ผ๐ ๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐
1. Find the cumulative sum of sales for each employee.
2. Rank employees based on their sales within their department.
3. Calculate a running total of orders by order date.
4. Identify the top three salaries in each department.
5. Compute the difference between the current and previous month's sales.
โค ๐๐ผ๐บ๐บ๐ผ๐ป ๐ง๐ฎ๐ฏ๐น๐ฒ ๐๐ ๐ฝ๐ฟ๐ฒ๐๐๐ถ๐ผ๐ป๐ (๐๐ง๐)
1. Write a recursive CTE to generate a sequence of numbers from 1 to 100.
2. Use a CTE to find employees who directly and indirectly report to a specific manager.
3. Calculate the factorial of a number using a recursive CTE.
4. Flatten a hierarchical organization chart using a CTE.
5. Use a CTE to calculate year-over-year growth in sales.
โค ๐๐ผ๐ถ๐ป๐ (๐๐ป๐ป๐ฒ๐ฟ, ๐ข๐๐๐ฒ๐ฟ, ๐๐ฟ๐ผ๐๐, ๐ฆ๐ฒ๐น๐ณ)
1. Retrieve a list of customers who have placed orders (Inner Join).
2. Find employees who have not been assigned to any projects (Left Join).
3. Get a list of projects without assigned employees (Right Join).
4. Generate all possible pairs of products (Cross Join).
5. Match employees to themselves to find pairs from the same department (Self Join).
โค ๐ฆ๐๐ฏ๐พ๐๐ฒ๐ฟ๐ถ๐ฒ๐
1. Find employees earning more than the average salary in their department.
2. Retrieve customers who have placed more than 5 orders.
3. List products that have never been ordered.
4. Identify the second highest salary in the company using a subquery.
5. Find departments where all employees earn above a specific threshold.
โค ๐๐ด๐ด๐ฟ๐ฒ๐ด๐ฎ๐๐ฒ ๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐
1. Calculate the total revenue generated by each product.
2. Find the average salary in each department.
3. Count the number of orders placed by each customer.
4. Find the maximum and minimum sales for each region.
5. Calculate the standard deviation of employee salaries.
โค ๐๐ป๐ฑ๐ฒ๐ ๐ถ๐ป๐ด ๐ฎ๐ป๐ฑ ๐ฃ๐ฒ๐ฟ๐ณ๐ผ๐ฟ๐บ๐ฎ๐ป๐ฐ๐ฒ
1. Identify queries that would benefit from indexing.
2. Compare execution plans with and without an index on a specific column.
3. Check which indexes exist on a table.
4. Optimize a slow query using indexing.
5. Write a query to update a column using an indexed lookup.
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
๐5โค3
Here are some tricky SQL interview questions!
1. Find the second-highest salary in a table without using LIMIT or TOP.
2. Write a SQL query to find all employees who earn more than their managers.
3. Find the duplicate rows in a table without using GROUP BY.
4. Write a SQL query to find the top 10% of earners in a table.
5. Find the cumulative sum of a column in a table.
6. Write a SQL query to find all employees who have never taken a leave.
7. Find the difference between the current row and the next row in a table.
8. Write a SQL query to find all departments with more than one employee.
9. Find the maximum value of a column for each group without using GROUP BY.
10. Write a SQL query to find all employees who have taken more than 3 leaves in a month.
These questions are designed to test your SQL skills, including your ability to write efficient queries, think creatively, and solve complex problems.
Here are the answers to these questions:
1. SELECT MAX(salary) FROM table WHERE salary NOT IN (SELECT MAX(salary) FROM table)
2. SELECT e1.* FROM employees e1 JOIN employees e2 ON e1.manager_id = (link unavailable) WHERE e1.salary > e2.salary
3. SELECT * FROM table WHERE rowid IN (SELECT rowid FROM table GROUP BY column HAVING COUNT(*) > 1)
4. SELECT * FROM table WHERE salary > (SELECT PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY salary) FROM table)
5. SELECT column, SUM(column) OVER (ORDER BY rowid) FROM table
6. SELECT * FROM employees WHERE id NOT IN (SELECT employee_id FROM leaves)
7. SELECT *, column - LEAD(column) OVER (ORDER BY rowid) FROM table
8. SELECT department FROM employees GROUP BY department HAVING COUNT(*) > 1
9. SELECT MAX(column) FROM table WHERE column NOT IN (SELECT MAX(column) FROM table GROUP BY group_column)
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
1. Find the second-highest salary in a table without using LIMIT or TOP.
2. Write a SQL query to find all employees who earn more than their managers.
3. Find the duplicate rows in a table without using GROUP BY.
4. Write a SQL query to find the top 10% of earners in a table.
5. Find the cumulative sum of a column in a table.
6. Write a SQL query to find all employees who have never taken a leave.
7. Find the difference between the current row and the next row in a table.
8. Write a SQL query to find all departments with more than one employee.
9. Find the maximum value of a column for each group without using GROUP BY.
10. Write a SQL query to find all employees who have taken more than 3 leaves in a month.
These questions are designed to test your SQL skills, including your ability to write efficient queries, think creatively, and solve complex problems.
Here are the answers to these questions:
1. SELECT MAX(salary) FROM table WHERE salary NOT IN (SELECT MAX(salary) FROM table)
2. SELECT e1.* FROM employees e1 JOIN employees e2 ON e1.manager_id = (link unavailable) WHERE e1.salary > e2.salary
3. SELECT * FROM table WHERE rowid IN (SELECT rowid FROM table GROUP BY column HAVING COUNT(*) > 1)
4. SELECT * FROM table WHERE salary > (SELECT PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY salary) FROM table)
5. SELECT column, SUM(column) OVER (ORDER BY rowid) FROM table
6. SELECT * FROM employees WHERE id NOT IN (SELECT employee_id FROM leaves)
7. SELECT *, column - LEAD(column) OVER (ORDER BY rowid) FROM table
8. SELECT department FROM employees GROUP BY department HAVING COUNT(*) > 1
9. SELECT MAX(column) FROM table WHERE column NOT IN (SELECT MAX(column) FROM table GROUP BY group_column)
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
โค5๐5
๐ง๐ผ๐ฝ ๐ ๐ก๐๐ ๐๐ถ๐ฟ๐ถ๐ป๐ด ๐๐ฎ๐๐ฎ ๐๐ป๐ฎ๐น๐๐๐๐ ๐
- Capgemini
- Infosys
- KPMG
- Genpact
- JP Morgan
Qualification :- Any Graduate
๐๐๐ ๐ข๐ฌ๐ญ๐๐ซ & ๐๐ฉ๐ฅ๐จ๐๐ ๐๐จ๐ฎ๐ซ ๐๐๐ฌ๐ฎ๐ฆ๐๐:-
https://bit.ly/3ZI20AY
Enter your experience & Complete The Registration Process
Select the company name & Apply for jobs
- Capgemini
- Infosys
- KPMG
- Genpact
- JP Morgan
Qualification :- Any Graduate
๐๐๐ ๐ข๐ฌ๐ญ๐๐ซ & ๐๐ฉ๐ฅ๐จ๐๐ ๐๐จ๐ฎ๐ซ ๐๐๐ฌ๐ฎ๐ฆ๐๐:-
https://bit.ly/3ZI20AY
Enter your experience & Complete The Registration Process
Select the company name & Apply for jobs
Netflix Analytics Engineer Interview Experience:
SQL Questions:
1๏ธโฃ SQL Question 1: Identify VIP Users for Netflix
Question: To better cater to its most dedicated users, Netflix would like to identify its โVIP usersโ - those who are most active in terms of the number of hours of content they watch. Write a SQL query that will retrieve the top 10 users with the most watched hours in the last month.
Tables:
โข users table: user_id (integer), sign_up_date (date), subscription_type (text)
โข watching_activity table: activity_id (integer), user_id (integer), date_time (timestamp), show_id (integer), hours_watched (float)
2๏ธโฃ SQL Question 2: Analyzing Ratings For Netflix Shows
Question: Given a table of user ratings for Netflix shows, calculate the average rating for each show within a given month. Assume that there is a column for user_id, show_id, rating (out of 5 stars), and date of review. Order the results by month and then by average rating (descending order).
Tables:
โข show_reviews table: review_id (integer), user_id (integer), review_date (timestamp), show_id (integer), stars (integer)
3๏ธโฃ SQL Question 3: What does EXCEPT / MINUS SQL commands do?
Question: Explain the purpose and usage of the EXCEPT (or MINUS in some SQL dialects) SQL commands.
4๏ธโฃ SQL Question 4: Filter Netflix Users Based on Viewing History and Subscription Status
Question: You are given a database of Netflixโs user viewing history and their current subscription status. Write a SQL query to find all active customers who watched more than 10 episodes of a show called โStranger Thingsโ in the last 30 days.
Tables:
โข users table: user_id (integer), active (boolean)
โข viewing_history table: user_id (integer), show_id (integer), episode_id (integer), watch_date (date)
โข shows table: show_id (integer), show_name (text)
5๏ธโฃ SQL Question 5: What does it mean to denormalize a database?
Question: Explain the concept and implications of denormalizing a database.
6๏ธโฃ SQL Question 6: Filter and Match Customerโs Viewing Records
Question: As a data analyst at Netflix, you are asked to analyze the customerโs viewing records. You confirmed that Netflix is especially interested in customers who have been continuously watching a particular genre - โDocumentaryโ over the last month. The task is to find the name and email of those customers who have viewed more than five โDocumentaryโ movies within the last month. โDocumentaryโ could be a part of a broader genre category in the genre field (for example, โDocumentary, Historyโ). Therefore, the matching pattern could occur anywhere within the string.
Tables:
โข movies table: movie_id (integer), title (text), genre (text), release_year (integer)
โข customer table: user_id (integer), name (text), email (text), last_movie_watched (integer), date_watched (date)
Here you can find essential SQL Interview Resources๐
https://t.iss.one/mysqldata
Like this post if you need more ๐โค๏ธ
Hope it helps :)
SQL Questions:
1๏ธโฃ SQL Question 1: Identify VIP Users for Netflix
Question: To better cater to its most dedicated users, Netflix would like to identify its โVIP usersโ - those who are most active in terms of the number of hours of content they watch. Write a SQL query that will retrieve the top 10 users with the most watched hours in the last month.
Tables:
โข users table: user_id (integer), sign_up_date (date), subscription_type (text)
โข watching_activity table: activity_id (integer), user_id (integer), date_time (timestamp), show_id (integer), hours_watched (float)
2๏ธโฃ SQL Question 2: Analyzing Ratings For Netflix Shows
Question: Given a table of user ratings for Netflix shows, calculate the average rating for each show within a given month. Assume that there is a column for user_id, show_id, rating (out of 5 stars), and date of review. Order the results by month and then by average rating (descending order).
Tables:
โข show_reviews table: review_id (integer), user_id (integer), review_date (timestamp), show_id (integer), stars (integer)
3๏ธโฃ SQL Question 3: What does EXCEPT / MINUS SQL commands do?
Question: Explain the purpose and usage of the EXCEPT (or MINUS in some SQL dialects) SQL commands.
4๏ธโฃ SQL Question 4: Filter Netflix Users Based on Viewing History and Subscription Status
Question: You are given a database of Netflixโs user viewing history and their current subscription status. Write a SQL query to find all active customers who watched more than 10 episodes of a show called โStranger Thingsโ in the last 30 days.
Tables:
โข users table: user_id (integer), active (boolean)
โข viewing_history table: user_id (integer), show_id (integer), episode_id (integer), watch_date (date)
โข shows table: show_id (integer), show_name (text)
5๏ธโฃ SQL Question 5: What does it mean to denormalize a database?
Question: Explain the concept and implications of denormalizing a database.
6๏ธโฃ SQL Question 6: Filter and Match Customerโs Viewing Records
Question: As a data analyst at Netflix, you are asked to analyze the customerโs viewing records. You confirmed that Netflix is especially interested in customers who have been continuously watching a particular genre - โDocumentaryโ over the last month. The task is to find the name and email of those customers who have viewed more than five โDocumentaryโ movies within the last month. โDocumentaryโ could be a part of a broader genre category in the genre field (for example, โDocumentary, Historyโ). Therefore, the matching pattern could occur anywhere within the string.
Tables:
โข movies table: movie_id (integer), title (text), genre (text), release_year (integer)
โข customer table: user_id (integer), name (text), email (text), last_movie_watched (integer), date_watched (date)
Here you can find essential SQL Interview Resources๐
https://t.iss.one/mysqldata
Like this post if you need more ๐โค๏ธ
Hope it helps :)
โค5๐4๐1
SQL is easy to learn, but difficult to master.
Here are 5 hacks to level up your SQL ๐
1. Know complex joins
2. Master Window functions
3. Explore alternative solutions
4. Master query optimization
5. Get familiar with ETL
โโโ
๐๐ต๐ธ, ๐ต๐ฉ๐ฆ๐ณ๐ฆ ๐ข๐ณ๐ฆ ๐ฑ๐ณ๐ข๐ค๐ต๐ช๐ค๐ฆ ๐ฑ๐ณ๐ฐ๐ฃ๐ญ๐ฆ๐ฎ๐ด ๐ช๐ฏ ๐ต๐ฉ๐ฆ ๐ค๐ข๐ณ๐ฐ๐ถ๐ด๐ฆ๐ญ.
๐ญ/ ๐๐ป๐ผ๐ ๐ฐ๐ผ๐บ๐ฝ๐น๐ฒ๐ ๐ท๐ผ๐ถ๐ป๐
LEFT JOIN, RIGHT JOIN, INNER JOIN, OUTER JOIN โ these are easy.
But SQL gets really powerful, when you know
โณ Anti Joins
โณ Self Joins
โณ Cartesian Joins
โณ Multi-Table Joins
๐ฎ/ ๐ ๐ฎ๐๐๐ฒ๐ฟ ๐ช๐ถ๐ป๐ฑ๐ผ๐ ๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐
Window functions = flexible, effective, and essential.
They give you Python-like versatility in SQL. ๐๐ถ๐ฑ๐ฆ๐ณ ๐ค๐ฐ๐ฐ๐ญ.
๐ฏ/ ๐๐ ๐ฝ๐น๐ผ๐ฟ๐ฒ ๐ฎ๐น๐๐ฒ๐ฟ๐ป๐ฎ๐๐ถ๐๐ฒ ๐๐ผ๐น๐๐๐ถ๐ผ๐ป๐
In SQL, thereโs rarely one โrightโ way to solve a problem.
By exploring alternative approaches, you develop flexibility in thinking AND learn about trade-offs.
๐ฐ/ ๐ ๐ฎ๐๐๐ฒ๐ฟ ๐พ๐๐ฒ๐ฟ๐ ๐ผ๐ฝ๐๐ถ๐บ๐ถ๐๐ฎ๐๐ถ๐ผ๐ป
Inefficient queries overload systems, cost money and waste time.
3 (super quick) tips on optimizing queries:
1. Use indexes effectively
2. Analyze execution plans
3. Reduce unnecessary operations
๐ฑ/ ๐๐ฒ๐ ๐ณ๐ฎ๐บ๐ถ๐น๐ถ๐ฎ๐ฟ ๐๐ถ๐๐ต ๐๐ง๐
ETL is the backbone of moving and preparing data.
โณ Extract: Pull data from various sources
โณ Transform: Clean, filter, and reformat the data
โณ Load: Store the cleaned data in a data warehouse
Here you can find essential SQL Interview Resources๐
https://t.iss.one/mysqldata
Like this post if you need more ๐โค๏ธ
Hope it helps :)
Here are 5 hacks to level up your SQL ๐
1. Know complex joins
2. Master Window functions
3. Explore alternative solutions
4. Master query optimization
5. Get familiar with ETL
โโโ
๐๐ต๐ธ, ๐ต๐ฉ๐ฆ๐ณ๐ฆ ๐ข๐ณ๐ฆ ๐ฑ๐ณ๐ข๐ค๐ต๐ช๐ค๐ฆ ๐ฑ๐ณ๐ฐ๐ฃ๐ญ๐ฆ๐ฎ๐ด ๐ช๐ฏ ๐ต๐ฉ๐ฆ ๐ค๐ข๐ณ๐ฐ๐ถ๐ด๐ฆ๐ญ.
๐ญ/ ๐๐ป๐ผ๐ ๐ฐ๐ผ๐บ๐ฝ๐น๐ฒ๐ ๐ท๐ผ๐ถ๐ป๐
LEFT JOIN, RIGHT JOIN, INNER JOIN, OUTER JOIN โ these are easy.
But SQL gets really powerful, when you know
โณ Anti Joins
โณ Self Joins
โณ Cartesian Joins
โณ Multi-Table Joins
๐ฎ/ ๐ ๐ฎ๐๐๐ฒ๐ฟ ๐ช๐ถ๐ป๐ฑ๐ผ๐ ๐ณ๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐
Window functions = flexible, effective, and essential.
They give you Python-like versatility in SQL. ๐๐ถ๐ฑ๐ฆ๐ณ ๐ค๐ฐ๐ฐ๐ญ.
๐ฏ/ ๐๐ ๐ฝ๐น๐ผ๐ฟ๐ฒ ๐ฎ๐น๐๐ฒ๐ฟ๐ป๐ฎ๐๐ถ๐๐ฒ ๐๐ผ๐น๐๐๐ถ๐ผ๐ป๐
In SQL, thereโs rarely one โrightโ way to solve a problem.
By exploring alternative approaches, you develop flexibility in thinking AND learn about trade-offs.
๐ฐ/ ๐ ๐ฎ๐๐๐ฒ๐ฟ ๐พ๐๐ฒ๐ฟ๐ ๐ผ๐ฝ๐๐ถ๐บ๐ถ๐๐ฎ๐๐ถ๐ผ๐ป
Inefficient queries overload systems, cost money and waste time.
3 (super quick) tips on optimizing queries:
1. Use indexes effectively
2. Analyze execution plans
3. Reduce unnecessary operations
๐ฑ/ ๐๐ฒ๐ ๐ณ๐ฎ๐บ๐ถ๐น๐ถ๐ฎ๐ฟ ๐๐ถ๐๐ต ๐๐ง๐
ETL is the backbone of moving and preparing data.
โณ Extract: Pull data from various sources
โณ Transform: Clean, filter, and reformat the data
โณ Load: Store the cleaned data in a data warehouse
Here you can find essential SQL Interview Resources๐
https://t.iss.one/mysqldata
Like this post if you need more ๐โค๏ธ
Hope it helps :)
Telegram
SQL For Data Analytics
This channel covers everything you need to learn SQL for data science, data analyst, data engineer and business analyst roles.
๐2๐1
35 Most Common SQL Interview Questions ๐๐
1.) Explain order of execution of SQL.
2.) What is difference between where and having?
3.) What is the use of group by?
4.) Explain all types of joins in SQL?
5.) What are triggers in SQL?
6.) What is stored procedure in SQL
7.) Explain all types of window functions?
(Mainly rank, row_num, dense_rank, lead & lag)
8.) What is difference between Delete and Truncate?
9.) What is difference between DML, DDL and DCL?
10.) What are aggregate function and when do we use them? explain with few example.
11.) Which is faster between CTE and Subquery?
12.) What are constraints and types of Constraints?
13.) Types of Keys?
14.) Different types of Operators ?
15.) Difference between Group By and Where?
16.) What are Views?
17.) What are different types of constraints?
18.) What is difference between varchar and nvarchar?
19.) Similar for char and nchar?
20.) What are index and their types?
21.) What is an index? Explain its different types.
22.) List the different types of relationships in SQL.
23.) Differentiate between UNION and UNION ALL.
24.) How many types of clauses in SQL?
25.) What is the difference between UNION and UNION ALL in SQL?
26.) What are the various types of relationships in SQL?
27.) Difference between Primary Key and Secondary Key?
28.) What is the difference between where and having?
29.) Find the second highest salary of an employee?
30.) Write retention query in SQL?
31.) Write year-on-year growth in SQL?
32.) Write a query for cummulative sum in SQL?
33.) Difference between Function and Store procedure ?
34.) Do we use variable in views?
35.) What are the limitations of views?
Here you can find essential SQL Interview Resources๐
https://t.iss.one/mysqldata
Like this post if you need more ๐โค๏ธ
Hope it helps :)
1.) Explain order of execution of SQL.
2.) What is difference between where and having?
3.) What is the use of group by?
4.) Explain all types of joins in SQL?
5.) What are triggers in SQL?
6.) What is stored procedure in SQL
7.) Explain all types of window functions?
(Mainly rank, row_num, dense_rank, lead & lag)
8.) What is difference between Delete and Truncate?
9.) What is difference between DML, DDL and DCL?
10.) What are aggregate function and when do we use them? explain with few example.
11.) Which is faster between CTE and Subquery?
12.) What are constraints and types of Constraints?
13.) Types of Keys?
14.) Different types of Operators ?
15.) Difference between Group By and Where?
16.) What are Views?
17.) What are different types of constraints?
18.) What is difference between varchar and nvarchar?
19.) Similar for char and nchar?
20.) What are index and their types?
21.) What is an index? Explain its different types.
22.) List the different types of relationships in SQL.
23.) Differentiate between UNION and UNION ALL.
24.) How many types of clauses in SQL?
25.) What is the difference between UNION and UNION ALL in SQL?
26.) What are the various types of relationships in SQL?
27.) Difference between Primary Key and Secondary Key?
28.) What is the difference between where and having?
29.) Find the second highest salary of an employee?
30.) Write retention query in SQL?
31.) Write year-on-year growth in SQL?
32.) Write a query for cummulative sum in SQL?
33.) Difference between Function and Store procedure ?
34.) Do we use variable in views?
35.) What are the limitations of views?
Here you can find essential SQL Interview Resources๐
https://t.iss.one/mysqldata
Like this post if you need more ๐โค๏ธ
Hope it helps :)
๐7
SQL, or Structured Query Language, is a domain-specific language used to manage and manipulate relational databases. Here's a brief A-Z overview by @sqlanalyst
A - Aggregate Functions: Functions like COUNT, SUM, AVG, MIN, and MAX used to perform operations on data in a database.
B - BETWEEN: A SQL operator used to filter results within a specific range.
C - CREATE TABLE: SQL statement for creating a new table in a database.
D - DELETE: SQL statement used to delete records from a table.
E - EXISTS: SQL operator used in a subquery to test if a specified condition exists.
F - FOREIGN KEY: A field in a database table that is a primary key in another table, establishing a link between the two tables.
G - GROUP BY: SQL clause used to group rows that have the same values in specified columns.
H - HAVING: SQL clause used in combination with GROUP BY to filter the results.
I - INNER JOIN: SQL clause used to combine rows from two or more tables based on a related column between them.
J - JOIN: Combines rows from two or more tables based on a related column.
K - KEY: A field or set of fields in a database table that uniquely identifies each record.
L - LIKE: SQL operator used in a WHERE clause to search for a specified pattern in a column.
M - MODIFY: SQL command used to modify an existing database table.
N - NULL: Represents missing or undefined data in a database.
O - ORDER BY: SQL clause used to sort the result set in ascending or descending order.
P - PRIMARY KEY: A field in a table that uniquely identifies each record in that table.
Q - QUERY: A request for data from a database using SQL.
R - ROLLBACK: SQL command used to undo transactions that have not been saved to the database.
S - SELECT: SQL statement used to query the database and retrieve data.
T - TRUNCATE: SQL command used to delete all records from a table without logging individual row deletions.
U - UPDATE: SQL statement used to modify the existing records in a table.
V - VIEW: A virtual table based on the result of a SELECT query.
W - WHERE: SQL clause used to filter the results of a query based on a specified condition.
X - (E)XISTS: Used in conjunction with SELECT to test the existence of rows returned by a subquery.
Z - ZERO: Represents the absence of a value in numeric fields or the initial state of boolean fields.
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
A - Aggregate Functions: Functions like COUNT, SUM, AVG, MIN, and MAX used to perform operations on data in a database.
B - BETWEEN: A SQL operator used to filter results within a specific range.
C - CREATE TABLE: SQL statement for creating a new table in a database.
D - DELETE: SQL statement used to delete records from a table.
E - EXISTS: SQL operator used in a subquery to test if a specified condition exists.
F - FOREIGN KEY: A field in a database table that is a primary key in another table, establishing a link between the two tables.
G - GROUP BY: SQL clause used to group rows that have the same values in specified columns.
H - HAVING: SQL clause used in combination with GROUP BY to filter the results.
I - INNER JOIN: SQL clause used to combine rows from two or more tables based on a related column between them.
J - JOIN: Combines rows from two or more tables based on a related column.
K - KEY: A field or set of fields in a database table that uniquely identifies each record.
L - LIKE: SQL operator used in a WHERE clause to search for a specified pattern in a column.
M - MODIFY: SQL command used to modify an existing database table.
N - NULL: Represents missing or undefined data in a database.
O - ORDER BY: SQL clause used to sort the result set in ascending or descending order.
P - PRIMARY KEY: A field in a table that uniquely identifies each record in that table.
Q - QUERY: A request for data from a database using SQL.
R - ROLLBACK: SQL command used to undo transactions that have not been saved to the database.
S - SELECT: SQL statement used to query the database and retrieve data.
T - TRUNCATE: SQL command used to delete all records from a table without logging individual row deletions.
U - UPDATE: SQL statement used to modify the existing records in a table.
V - VIEW: A virtual table based on the result of a SELECT query.
W - WHERE: SQL clause used to filter the results of a query based on a specified condition.
X - (E)XISTS: Used in conjunction with SELECT to test the existence of rows returned by a subquery.
Z - ZERO: Represents the absence of a value in numeric fields or the initial state of boolean fields.
Here you can find essential SQL Interview Resources๐
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
Like this post if you need more ๐โค๏ธ
Hope it helps :)
โค7๐5