Browsing:

Category: SQL

The order of SQL execution

The order of SQL execution follows a specific logical sequence, regardless of how the SQL query is written. Here’s the general execution order of SQL clauses: Visualizing the SQL Execution Order: sqlCopy codeSELECT column_list FROM table JOIN another_table ON condition Read more…


NOT IN and NOT EXISTS

Scenario: Imagine you have two tables: students and enrollments. The students table contains all students, and the enrollments table contains the students who are enrolled in courses. We want to find out which students are not enrolled in any course. Read more…


SQL -problem -subscription

Source:https://www.interviewquery.com/questions/subscription-overlap Given a table of product subscriptions with a subscription start date and end date for each user, write a query that returns true or false whether or not each user has a subscription date range that overlaps with any Read more…


CROSS JOIN and UNION ALL

Let’s say you have the following table: user_id start_date end_date 1 2023-01-01 2023-01-10 2 2023-01-05 2023-01-15 3 2023-02-01 2023-02-10 Why CROSS JOIN? Why Not UNION ALL? Key Limitation: Alternative to CROSS JOIN (More Efficient Approach): If you want to avoid Read more…


“Group by” with window functions vs traditional method

Both SQL queries you’ve mentioned are similar in terms of their goal (calculating the sum of salaries), but they differ in how they operate and what results they return. 1. Query 1: Grouping by department_id What it does: Example Output: Read more…


SQL question -time interval (seven-day streak of visiting the same URL)

Given a table with event logs, find the percentage of users that had at least one seven-day streak of visiting the same URL. Note: Round the results to 2 decimal places. For example, if the result is 35.67% return 0.35. Read more…


SQL window function with “ORDER BY” and without “ORDER BY”

Dataset Example: Assume we have the following sales data for different employees across several months: emp_id sales month 1 100 Jan 2 200 Jan 3 300 Jan 4 150 Feb 5 250 Feb 6 350 Mar 7 400 Mar Now, Read more…


GROUP BY vs. WINDOW FUNCTIONS

These two queries use different SQL techniques (GROUP BY vs. WINDOW FUNCTIONS) and have distinct purposes, even though they both work with the same dataset. Here’s a detailed comparison: 1. First Query (Using GROUP BY): What it does: Output: Use Read more…


SQL questions

2. To delete duplicates by keeping first record 3. List all employees whose salaries are above their manager’s salary employee (emp_id, salary, dept_id, manager_id) , manager also could be an employee homself Explanation: This query will return all employees whose Read more…


SQL problem to solve (window function)

Source for problem:https://www.interviewquery.com/questions/user-experience-percentage?via=venkata&ref=venkata my solution :