Table
departments
5 columns · 0 foreign keys · referenced by 0 tables
Columns
| # | Name | Type | Key |
|---|---|---|---|
| 01 | dept_id | SERIAL | PK |
| 02 | dept_name | VARCHAR(100) | |
| 03 | location | VARCHAR(100) | |
| 04 | budget | NUMERIC | |
| 05 | created_at | TIMESTAMP |
Example queries against this table
- Select all departments and their locationsSQL
SELECT dept_name, location, budget FROM departments ORDER BY dept_name ASC; - INNER JOIN: employees with their department namesSQL
SELECT e.emp_id, e.first_name, e.last_name, d.dept_name, d.location FROM employees e INNER JOIN departments d ON e.dept_id = d.dept_id; - LEFT JOIN: all employees even if no departmentSQL
SELECT e.first_name, e.last_name, COALESCE(d.dept_name, 'Unassigned') AS department FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id; - RIGHT JOIN: show all departments even without employeesSQL
SELECT d.dept_name, d.location, COUNT(e.emp_id) AS employee_count FROM employees e RIGHT JOIN departments d ON e.dept_id = d.dept_id GROUP BY d.dept_id, d.dept_name, d.location ORDER BY employee_count;