Tutor SQL (@TutorSql) profile picture

Find answers to commonly asked SQL interview questions.

Joined July 2023
How can you find the 3rd highest salary in Oracle using an analytic function? #DenseRank #SQL #TutorSQL #Oracle #Analytics #Database #DataAnalyst #DataScientist
15
Q9: Write an SQL query using CASE to calculate the grade for each student: If the avg of math and science scores is greater than or equal to 90, assign A grade. If the avg is between 80 & 89, assign B grade If the avg is between 70 & 79, assign C grade Otherwise, assign 'D'.
1
45
Q8: Write an SQL query to calculate the month-over-month percentage growth for each product's sales. You should retrieve the product_id, sale_date, and percentage growth from the previous month's sales using the LEAD and LAG functions.
1
37
In this query, we use the LEAD and LAG functions to access values from the previous and next rows. The PARTITION BY clause is used with LEAD and LAG to calculate the values within each partition of the specified column (in this case, product_id).
1
5
Protip: 😎 Do you know that using the LEAD and LAG functions effectively and considering both the PARTITION BY and ORDER BY clauses, you can perform robust time-based and comparative analysis on your data in Oracle SQL. 🚀
1
81
Q7: In Oracle SQL, the LIMIT function is unavailable, but there's an equivalent approach using the ROWNUM technique. Consider a table named "products" as per the screenshot. Write an SQL query to retrieve the top 3 cheapest products using the ROWNUM technique (equi. to LIMIT).
1
47
The inner query retrieves all rows from the "products" table and sorts them in asc order based on the "price" using the ORDER BY clause. The outer query then selects the rows from the result of the inner query where the ROWNUM is <= 3, limiting to the top 3 cheapest products.
1
14
Question #6: Imagine you have a table named "events" with the attached structure and data: Write an SQL query to extract the hours, minutes, and seconds from the "event_date" column and display them in separate columns in the result set. Hint: Use the TO_CHAR function.
1
33
The TO_CHAR function extracts and formats the hours, minutes, and seconds from the "event_date". The 'HH24' format specifier extracts the hours in 24-hour format with leading zeros, 'MI' extracts the minutes with leading zeros, and 'SS' extracts the seconds with leading zeros.
1
4
Question 5: write an SQL query to find the total number of orders placed by each customer and display the results in descending order based on the number of orders. If two or more customers have the same number of orders, sort them in ascending order based on their customer_id.
1
24
Use the COUNT func to calculate each customer's total number of orders. Use the GROUP BY clause to group the results by customer_id. Sort first by the total no. of orders in desc, and if there are ties, then sort the customers in asc order. Expected Output in the picture.
1
9
Question 4: You have a table named "employees" and data as given in the attachment. Write an SQL query to extract the first 2 char from "first_name" and the last 3 char from "last_name" for each employee.
1
31
The query should combine these extracted substrings to create a new column named "user_id." The "user_id" should be displayed in the result set attachment. You must use the SUBSTR function to extract the specified characters from the "first_name" and "last_name" columns.
1
4
Question: Imagine you have a table named "sales" with the following structure and data: order_id order_date 1 2023-07-15 08:30:00 2 2023-06-20 13:45:00 Write an SQL query that extracts the year, month, and day from the "order_date".
1
28
The EXTRACT function in Oracle SQL extracts a specific component (such as year, month, day, hour, etc.) from a date or timestamp. It allows you to extract individual parts of a date for further analysis or filtering. The answer screenshots are attached.
1
21
Example of a SQL query that can determine the second highest salary from a salary table. Let's say the salary table looks like this: amount 50000 60000 45000 75000 55000 The query will result in the second-highest salary, which is 60000. #SQLMastery #DataAnalysis #LearnSQL
21
💻 Say goodbye to complex subqueries and hello to efficiency! 🚀 Here's a quick example: Calculate the average salary within each department using 'salary' column, PARTITIONED BY 'department,' #SQLMastery #DataAnalysis #OVERPARTITION #LearnSQL #TutorSql
23