Practice these queries to sharpen your SQL skills. If you get stuck, take help from Google or other resources.


Sample Dataset: users Table

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  first_name VARCHAR(50),
  last_name VARCHAR(50),
  age INT,
  country VARCHAR(50),
  email VARCHAR(100)
);

INSERT INTO users (first_name, last_name, age, country, email) VALUES
('Arif',    'Hossain',   25, 'Bangladesh', '[email protected]'),
('Sara',    'Khan',      30, 'India',      '[email protected]'),
('Rafi',    'Ahmed',     22, 'Pakistan',   '[email protected]'),
('Nusrat',  'Akter',     28, 'Nepal',      '[email protected]'),
('Tanvir',  'Rahman',    35, 'Sri Lanka',  '[email protected]'),
('Mina',    'Begum',     20, 'Maldives',   '[email protected]'),
('Fahim',   'Chowdhury', 40, 'Bhutan',     '[email protected]'),
('Rumana',  'Sultana',   27, 'Afghanistan','[email protected]'),
('Jahid',   'Islam',     24, 'Bangladesh', '[email protected]'),
('Tania',   'Sultana',   29, 'Nepal',      '[email protected]');


Simple Queries

  1. Select all users
  2. Show only first name and email
  3. Find users older than 25
  4. Count how many users are from each country
  5. Find the youngest user’s age
  6. Find the oldest user’s age
  7. Find the average age of all users
  8. List users ordered by age (youngest first)

Add Some NULL Data for COALESCE Practice

Let’s say some users don’t have an email yet:

INSERT INTO users (first_name, last_name, age, country, email) VALUES
('Imran', 'Haque', 26, 'Bangladesh', NULL),
('Lina',  'Begum', 23, 'India', NULL);


Queries with COALESCE