Skip to content
skillbld
All interview topics

SQL interview questions with queries you can run and edit in real PostgreSQL in your browser: joins, aggregation, window functions, classic problems, indexes and performance, transactions and database design.

  • 50 questions
  • 15 easy
  • 28 medium
  • 7 hard
  • Free, no sign-up
Sample database for the runnable queriesShow

Every query on this page runs in real PostgreSQL inside your browser. Each question starts with a fresh copy of these tables, so you can edit and run freely.

setup.sql
-- A small company: staff, customers and their orders.
 
CREATE TABLE departments (
  id   int PRIMARY KEY,
  name text NOT NULL
);
 
CREATE TABLE employees (
  id            int PRIMARY KEY,
  name          text NOT NULL,
  department_id int REFERENCES departments (id),
  manager_id    int REFERENCES employees (id),
  salary        numeric(10, 2) NOT NULL,
  hired_on      date NOT NULL
);
 
CREATE TABLE customers (
  id        int PRIMARY KEY,
  name      text NOT NULL,
  city      text,
  signed_up date NOT NULL
);
 
CREATE TABLE products (
  id       int PRIMARY KEY,
  name     text NOT NULL,
  category text NOT NULL,
  price    numeric(10, 2) NOT NULL
);
 
CREATE TABLE orders (
  id          int PRIMARY KEY,
  customer_id int NOT NULL REFERENCES customers (id),
  ordered_on  date NOT NULL,
  status      text NOT NULL
);
 
CREATE TABLE order_items (
  order_id   int REFERENCES orders (id),
  product_id int REFERENCES products (id),
  quantity   int NOT NULL,
  unit_price numeric(10, 2) NOT NULL,
  PRIMARY KEY (order_id, product_id)
);
 
INSERT INTO departments VALUES
  (1, 'Engineering'), (2, 'Sales'), (3, 'Support'), (4, 'Legal');
 
INSERT INTO employees VALUES
  (1, 'Maya',  1, NULL, 185000, '2018-02-01'),
  (2, 'Arjun', 1, 1,    142000, '2019-06-15'),
  (3, 'Lena',  1, 1,    142000, '2021-03-08'),
  (4, 'Tom',   1, 2,     98000, '2023-01-09'),
  (5, 'Sofia', 2, 1,    120000, '2019-11-20'),
  (6, 'Kofi',  2, 5,     76000, '2022-04-04'),
  (7, 'Priya', 2, 5,     81000, '2021-09-13'),
  (8, 'Diego', 3, 1,     64000, '2020-07-27'),
  (9, 'Hana',  3, 8,     52000, '2024-02-19'),
  (10, 'Sam',  NULL, 1,  70000, '2024-05-06');
 
INSERT INTO customers VALUES
  (1, 'Acme Corp',    'London', '2023-01-15'),
  (2, 'Globex',       'Berlin', '2023-03-02'),
  (3, 'Initech',      'London', '2023-07-21'),
  (4, 'Umbrella',     'Paris',  '2024-01-10'),
  (5, 'Hooli',        NULL,     '2024-02-28'),
  (6, 'Stark Supply', 'Berlin', '2024-06-05');
 
INSERT INTO products VALUES
  (1, 'Laptop',     'Hardware', 1200.00),
  (2, 'Monitor',    'Hardware',  300.00),
  (3, 'Keyboard',   'Hardware',   80.00),
  (4, 'Office 365', 'Software',  100.00),
  (5, 'Antivirus',  'Software',   40.00),
  (6, 'Setup',      'Services',  150.00);
 
INSERT INTO orders VALUES
  (101, 1, '2024-01-05', 'shipped'),
  (102, 2, '2024-01-18', 'shipped'),
  (103, 1, '2024-02-02', 'shipped'),
  (104, 3, '2024-02-14', 'cancelled'),
  (105, 4, '2024-02-20', 'shipped'),
  (106, 1, '2024-03-03', 'pending'),
  (107, 2, '2024-03-11', 'shipped'),
  (108, 5, '2024-03-25', 'shipped'),
  (109, 3, '2024-03-28', 'pending');
 
INSERT INTO order_items VALUES
  (101, 1, 2, 1200.00), (101, 4, 2, 100.00),
  (102, 2, 3,  300.00), (102, 3, 3,  80.00),
  (103, 1, 1, 1150.00), (103, 6, 1, 150.00),
  (104, 5, 10,  40.00),
  (105, 1, 1, 1200.00), (105, 2, 2, 300.00), (105, 5, 1, 40.00),
  (106, 3, 5,   80.00),
  (107, 4, 4,  100.00), (107, 6, 2, 150.00),
  (108, 2, 1,  300.00),
  (109, 1, 1, 1200.00), (109, 4, 1, 100.00);
50 of 50 questions