#272 SQL Queries for Employees and Exercises

Companies:
Sberbank
Themes:
SQLJoinsAggregationPractice

The following tables are given:

CREATE TABLE employees (
    id INT PRIMARY KEY,
    head_id INT,
    name VARCHAR(1000),
    age INT,
    CONSTRAINT employees_head_fk
        FOREIGN KEY (head_id) REFERENCES employees(id)
);

CREATE TABLE exercises (
    id INT PRIMARY KEY,
    name VARCHAR(1000),
    employee_id INT,
    created_at DATE,
    CONSTRAINT exercises_employee_fk
        FOREIGN KEY (employee_id) REFERENCES employees(id)
);

It is necessary to:

  • Get all employees whose name begins with Ivanov or Petrov and whose age is greater than 18.
  • Count the number of exercises during the last 6 months for each employee. Consider only employees aged from 25 to 35. Display the manager name, employee name, and exercise count. Skip employees with fewer than three exercises. Sort by exercise count in descending order.
  • Get the three latest exercises for every employee: id, employee_name, exercise_name, and created_at.

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