PromptBase
PromptBase SelectGet Select
Close icon
General
Home
Marketplace
Create
Hire
Login
Chat
Sell
Explore

Prompt Details

Model
Chat - (gpt-6-luna)
Token size
1,372
Example input
[DATABASE_SYSTEM]: Oracle Database 19c [SCHEMA]: Table DEPARTMENTS: DEPARTMENT_ID (primary key), DEPARTMENT_NAME. Table EMPLOYEES: EMPLOYEE_ID (primary key), FIRST_NAME, LAST_NAME, DEPARTMENT_ID (foreign key to DEPARTMENTS.DEPARTMENT_ID), SALARY. A department can have zero, one or many employees. [REQUEST_TYPE]: GENERATE [SQL_INPUT]: Write a query that lists every department with its department ID, department name, employee count and average employee salary. Include departments that currently have no employees. Sort by department name in ascending order. [EXPECTED_BEHAVIOR]: Return one row per department. Departments with no employees must still appear with an employee count of 0. Their average salary should remain NULL rather than being represented as a real salary of 0. Round non-NULL average salary to two decimal places.
Example output
# **GENERATE — Oracle Database 19c** ## **1. SQL solution** Oracle 19c compatible ``` SELECT d.DEPARTMENT_ID, d.DEPARTMENT_NAME, COUNT(e.EMPLOYEE_ID) AS EMPLOYEE_COUNT, ROUND(AVG(e.SALARY), 2) AS AVG_SALARY FROM DEPARTMENTS d LEFT JOIN EMPLOYEES e ON d.DEPARTMENT_ID = e.DEPARTMENT_ID GROUP BY d.DEPARTMENT_ID, d.DEPARTMENT_NAME ORDER BY d.DEPARTMENT_NAME ASC; ``` ## **2. How it works** | SQL component | Purpose | | ---------------------- | ------------------------------------------------- | | `LEFT JOIN` | Includes all departments, even without employees. | | `COUNT(e.EMPLOYEE_ID)` | Counts employees. Returns 0 when none exist. | | `AVG(e.SALARY)` | Calculates average salary, ignoring NULL values. | | `ROUND(..., 2)` | Rounds average salary to two decimal places. | | `GROUP BY` | Produces one row per department. | | `ORDER BY ... ASC` | Sorts department names alphabetically. | **Why **`LEFT JOIN`** instead of **`INNER JOIN`**?** - `INNER JOIN` excludes departments without employees. - `LEFT JOIN` preserves every department. **Why **`COUNT(e.EMPLOYEE_ID)`** instead of **`COUNT(*)`**?** For a department without employees, the LEFT JOIN still produces one row with NULL employee columns. - `COUNT(*)` would return 1. - `COUNT(e.EMPLOYEE_ID)` correctly returns 0. **Why does average salary remain NULL?** `AVG()` ignores NULL values. When a department has no employees, there are no salary values to average, so the result remains NULL. ## **3. Assumptions and schema dependencies** - `DEPARTMENTS.DEPARTMENT_ID` is the primary key. - `EMPLOYEES.EMPLOYEE_ID` is the primary key and is non-NULL. - `EMPLOYEES.DEPARTMENT_ID` references `DEPARTMENTS.DEPARTMENT_ID`. - No additional filters are required. ## **4. Validation checklist** Test these scenarios in your database: | Test case | Expected employee count | Expected average salary | | ----------------------------------------------------- | ----------------------- | ---------------------------- | | Department with 5 employees | 5 | Average of salaries | | Department with 1 employee | 1 | That employee's salary | | Department with 0 employees | 0 | NULL | | Department with employees whose salaries are all NULL | Employee count | NULL | | Department with mixed NULL and non-NULL salaries | Employee count | Average of non-NULL salaries | ## **5. Safety and performance notes** - This is a read-only `SELECT` query. - No additional indexes are required solely to make this query correct. - The existing primary-key indexes may help, but actual performance depends on table size, statistics and the execution plan. Final review passed  The query preserves all departments, correctly handles zero employees and NULL salaries, and uses Oracle 19c-compatible SQL. It has not been executed against your database.
GPT

Sqlfix Sql Generator Debugger

Add to Cart
Instant accessInstant access
Usage rightsCommercial use
Money-back guaranteeMoney‑back
By purchasing this prompt, you agree to our terms of service
GPT-6-LUNA
Tested icon
Guide icon
4 examples icon
Free credits icon
Generate, debug, explain and improve SQL queries with SQLFix AI. Choose your database (Oracle, MySQL, PostgreSQL, SQL Server or SQLite), describe your task, paste your table schema and provide an existing query or error message. Get corrected or generated SQL, step-by-step explanations, assumptions, edge cases and practical performance suggestions. Designed for students, SQL learners and developers. Verify queries against your own database before execution.
...more
Added 11 hours ago
Report
Browse Marketplace