#214 Aggregating Employee Salaries by Department

Companies:
Aston
Themes:
SQLJoinsAggregationPractice

Display the average salary by department:

CREATE TABLE IF NOT EXISTS department (
    id BIGSERIAL PRIMARY KEY NOT NULL,
    name VARCHAR(254) UNIQUE NOT NULL
);

CREATE TABLE IF NOT EXISTS employee (
    id BIGSERIAL PRIMARY KEY NOT NULL,
    first_name VARCHAR(254) NOT NULL,
    last_name VARCHAR(254) NOT NULL,
    salary NUMERIC(10, 2) NOT NULL CHECK(salary > 0),
    department_id BIGSERIAL NOT NULL CONSTRAINT employee_department_id_fk REFERENCES department (id)
    ON DELETE CASCADE
);

INSERT INTO department(name)
VALUES
    ('BA & SA'),
    ('Java'),
    ('DevOps'),
    ('Android'),
    ('QA');

INSERT INTO employee(first_name, last_name, salary, department_id)
VALUES
    ('Alex', 'Petrov', 1000, 1),
    ('Andrey', 'Gromov', 1700, 1),
    ('Ivan', 'Sidorov', 2000, 2),
    ('Jane', 'Brown', 3000, 3),
    ('Maksim', 'Rudov', 5000, 3),
    ('Iryna', 'Somova', 2500, 2),
    ('Mike', 'Ostin', 1500, 4),
    ('Roman', 'Ivanov', 1500, 5);

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in