OfferGenie
All Questions

How can you write a SQL query to show all department names with their total revenue for the past twelve months?

NetflixTechnicalDifficulty: Medium
Share on

Ready to answer it out loud?

Run a mock interview on this exact question and get instant AI feedback.

Practice this question

Question Explain

To write a SQL query that displays all department names along with their total revenue over the past twelve months, you need to consider the database schema, which typically includes at least two tables: one for departments and another for transactions or sales that record revenue. Below is a step-by-step explanation of how to construct such a query, assuming you have a departments table and a sales table.

  1. Understand the Tables:

    • Departments Table: This table should at least have columns for department_id and department_name.
    • Sales Table: This should include columns such as sale_id, department_id, amount, and sale_date.
  2. Join the Tables:

    • Use an INNER JOIN to connect the departments table with the sales table on the department_id field.
  3. Filter for the Last Twelve Months:

    • Use a WHERE clause to filter the sales table for transactions that occurred within the past twelve months. This can be dynamically calculated using the current date functions provided by SQL (such as CURRENT_DATE) and date arithmetic.
  4. Aggregate the Data:

    • Use the SUM() function to calculate the total revenue for each department.
    • Group the results by department_name to ensure that the aggregation is performed for each department.
  5. Select and Order the Results:

    • Finally, select the department names and their corresponding total revenues, and optionally order the results for better readability.

Here is an example SQL query that follows this logic:

SELECT 
    d.department_name,
    SUM(s.amount) AS total_revenue
FROM 
    departments d
INNER JOIN 
    sales s ON d.department_id = s.department_id
WHERE 
    s.sale_date >= DATE_SUB(CURRENT_DATE, INTERVAL 12 MONTH)
GROUP BY 
    d.department_name
ORDER BY 
    total_revenue DESC;
  • Explanation:
    • INNER JOIN connects the departments and sales tables based on department_id.
    • WHERE s.sale_date >= DATE_SUB(CURRENT_DATE, INTERVAL 12 MONTH) filters sales records to include only those from the past twelve months.
    • SUM(s.amount) AS total_revenue calculates the total revenue for each department.
    • GROUP BY d.department_name groups the results by department names for aggregation.
    • ORDER BY total_revenue DESC sorts the results by total revenue in descending order, so the department with the highest revenue appears first.

This query assumes that the date functions and syntax used (DATE_SUB and CURRENT_DATE) are supported by your SQL database (e.g., MySQL). If you are using a different database system, you might need to adjust the date functions accordingly.

Answer Example

To write a SQL query that displays all department names along with their total revenue for the past twelve months, you'll need to understand your database schema and construct the query accordingly. Typically, your database could have the following structure:

  1. Database Schema:

    • Departments Table:
      • department_id: The unique identifier for each department.
      • department_name: The name of the department.
    • Sales Table:
      • sale_id: The unique identifier for each sale transaction.
      • department_id: The identifier linking the sale to a department.
      • amount: The revenue amount of the sale.
      • sale_date: The date when the sale occurred.
  2. Steps to Construct the SQL Query:

    • Join the Tables: You'll need to perform an INNER JOIN on the departments table and sales table using the department_id as the linking field.

    • Filter for the Last Twelve Months: Use a WHERE clause to filter sales that occurred in the last twelve months. Use SQL date functions like CURRENT_DATE to get the current date and perform date arithmetic to filter appropriately.

    • Aggregate by Department: Use the SUM() function to calculate the total revenue for each department and group the results by department_name.

    • Order Results: Finally, order the results by total revenue to see which department made the most.

Here is a sample query that demonstrates these steps:

SELECT 
    d.department_name,
    SUM(s.amount) AS total_revenue
FROM 
    departments d
INNER JOIN 
    sales s ON d.department_id = s.department_id
WHERE 
    s.sale_date >= DATE_SUB(CURRENT_DATE, INTERVAL 12 MONTH)
GROUP BY 
    d.department_name
ORDER BY 
    total_revenue DESC;

Explanation:

  • Join: INNER JOIN combines rows from departments and sales where department_id matches.
  • Filter: s.sale_date >= DATE_SUB(CURRENT_DATE, INTERVAL 12 MONTH) limits the results to sales within the last twelve months.
  • Aggregate: SUM(s.amount) AS total_revenue computes the total revenue per department.
  • Group and Order: GROUP BY d.department_name groups results for each department, and ORDER BY total_revenue DESC sorts departments by their revenue from highest to lowest.

Note: Ensure that your SQL dialect supports DATE_SUB and CURRENT_DATE functions as used in this query. Adjust the date functions if you're using a different SQL dialect.