TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: SQL Programming Test in 2024
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
MySQL TutorialSQL Interview

SQL Programming Test in 2024

Last updated: May 26, 2024 3:41 pm
By Meenakshi Agarwal
Share
11 Min Read
SQL Programming Test in 2023
SHARE

Welcome to this amazing SQL programming test that will help anyone deeply interested in working with databases. This test will help you assess your strengths and find gaps in your SQL skills. Ultimately, you’ll know which part of the SQL you should focus on.

Contents
Sample Test Data for SQL QueriesSQL Programming TestBeginner LevelIntermediate LevelAdvanced LevelSummary

Test Your SQL Programming Skills

Before you delve into the questions and answer section, make sure you have a SQL set up or use some online SQL compiler. Here is the test data you should create as it is used for writing the SQL queries.

Sample Test Data for SQL Queries

Here is sample test data for the SQL programming test.

Context: Galaxy-Themed E-Commerce Store Database

Tables: cust_tbl, ord_tbl, and prod_tbl

cust_idcust_nameemailjoin_date
1Samsam@email.com2022-01-15
2Kimkim@email.com2022-02-20
3Kanekane@email.com2022-03-25
4Davedave@email.com2022-04-10
Cust_tbl Table
ord_idcust_idord_datettl_amt
10112022-04-01100.00
10222022-04-0575.50
10312022-04-1045.25
10432022-05-02150.75
10522022-05-0565.00
10642022-05-10200.50
Ord_tbl Table
prod_idprod_nameunit_price
1Laptop800.00
2Phone400.00
3Tablet300.00
4Headphones50.00
5Mouse15.00
6Keyboard30.00
Prod_tbl Table

You can create the sample data for the cust_tbl, ord_tbls, and prod_tbl tables using SQL statements like this:

  • Create the cust_tbl Table:
CREATE TABLE cust_tbl (
    cust_id INT PRIMARY KEY,
    cust_name VARCHAR(255),
    email VARCHAR(255),
    join_date DATE
);

INSERT INTO cust_tbl (cust_id, cust_name, email, join_date)
VALUES
    (1, 'Sam', 'sam@email.com', '2022-01-15'),
    (2, 'Kim', 'kim@email.com', '2022-02-20'),
    (3, 'Kane', 'kane@email.com', '2022-03-25'),
    (4, 'Dave', 'dave@email.com', '2022-04-10');
  • Create the ord_tbls Table:
CREATE TABLE ord_tbls (
    ord_id INT PRIMARY KEY,
    cust_id INT,
    ord_date DATE,
    ttl_amt DECIMAL(10, 2),
    FOREIGN KEY (cust_id) REFERENCES cust_tbl(cust_id)
);

INSERT INTO ord_tbls (ord_id, cust_id, ord_date, ttl_amt)
VALUES
    (101, 1, '2022-04-01', 100.00),
    (102, 2, '2022-04-05', 75.50),
    (103, 1, '2022-04-10', 45.25),
    (104, 3, '2022-05-02', 150.75),
    (105, 2, '2022-05-05', 65.00),
    (106, 4, '2022-05-10', 200.50);
  • Create the prod_tbl Table:
CREATE TABLE prod_tbl (
    prod_id INT PRIMARY KEY,
    prod_name VARCHAR(255),
    unit_price DECIMAL(10, 2)
);

INSERT INTO prod_tbl (prod_id, prod_name, unit_price)
VALUES
    (1, 'Laptop', 800.00),
    (2, 'Phone', 400.00),
    (3, 'Tablet', 300.00),
    (4, 'Headphones', 50.00),
    (5, 'Mouse', 15.00),
    (6, 'Keyboard', 30.00);

These SQL statements will create the tables and insert the sample data. Adjust the data as needed for your tests.

SQL Programming Test

We hope you created the above sample data. You are all set to start facing the questions given below. However, if you see more test data is needed, add it yourself.

Also Read: SQL Complex Queries You Must Try

Beginner Level

Firstly, let’s face the beginner-level SQL programming test questions. Make sure you have created the demo data.

  1. Retrieve the names of every customer who joined before ‘2022-03-25’.
    Ans.
   SELECT cust_name
   FROM cust_tbl
   WHERE join_date < '2022-03-25';
  1. List out every order made by “Kim.”
    Ans.
   SELECT ord_id, ord_date, ttl_amt
   FROM ord_tbls
   WHERE cust_id = (SELECT cust_id FROM cust_tbl WHERE cust_name = 'Kim');
  1. Calculate the number of products that are present in the prod_tbl.
    Ans.
   SELECT COUNT(*) AS Total_Products_Present
   FROM prod_tbl;
  1. Find out how many orders were booked as per the data in the ord_tbls.
    Ans.
   SELECT COUNT(*) AS Total_Orders_Booked
   FROM ord_tbls;
  1. What is the average order amount for customers who joined before ‘2022-03-25’?
    Ans.
   SELECT AVG(ttl_amt) AS Avg_Order_Amt
   FROM ord_tbls
   WHERE cust_id IN (SELECT cust_id FROM cust_tbl WHERE join_date < '2022-03-25');
  1. Which product is the most popular among those customers who joined in April 2022?
    Ans.
   SELECT prod_name
   FROM prod_tbl
   WHERE prod_id = (
       SELECT prod_id
       FROM ord_tbls
       WHERE cust_id IN (SELECT cust_id FROM cust_tbl WHERE join_date >= '2022-04-01' AND join_date < '2022-05-01')
       GROUP BY prod_id
       ord_tbl BY COUNT(*) DESC
       LIMIT 1
   );
  1. What is the total revenue generated from orders placed in May 2022?
    Ans.
   SELECT SUM(ttl_amt) AS Total_Revenue
   FROM ord_tbls
   WHERE ord_date >= '2022-05-01' AND ord_date < '2022-06-01';
  1. What are the total orders placed by customers who have spent more than $500 on the store?
    Ans.
   SELECT COUNT(*) AS Order_Count
   FROM ord_tbls
   WHERE cust_id IN (
       SELECT cust_id
       FROM ord_tbls
       GROUP BY cust_id
       HAVING SUM(ttl_amt) > 500
   );

Check This: 25 SQL Interview Questions and Answers for 5+ Years

Intermediate Level

Secondly, let’s evaluate our skills with intermediate-level SQL programming test questions.

  1. Find the customers who have booked more than two orders.
    Ans.
   SELECT cust_name
   FROM cust_tbl
   WHERE cust_id IN (
       SELECT cust_id
       FROM ord_tbls
       GROUP BY cust_id
       HAVING COUNT(*) > 2
   );
  1. Calculate the total amount spent by each customer in April 2022.
    Ans.
   SELECT cust_name, SUM(ttl_amt) AS total_spent
   FROM cust_tbl
   JOIN ord_tbls ON cust_tbl.cust_id = ord_tbls.cust_id
   WHERE ord_date >= '2022-04-01' AND ord_date < '2022-05-01'
   GROUP BY cust_name;
  1. List every product with a unit price between $50.00 and $400.00.
    Ans.
   SELECT prod_name, unit_price
   FROM prod_tbl
   WHERE unit_price BETWEEN 50.00 AND 400.00;
  1. Find the customers who placed orders in both April and May 2022.
    Ans.
   SELECT cust_name
   FROM cust_tbl
   WHERE cust_id IN (
       SELECT cust_id
       FROM ord_tbls
       WHERE ord_date >= '2022-04-01' AND ord_date < '2022-05-01'
       INTERSECT
       SELECT cust_id
       FROM ord_tbls
       WHERE ord_date >= '2022-05-01' AND ord_date < '2022-06-01'
   );
  1. Find the top 5 customers who have spent the most on their orders in the past 6 months.
    Ans.
   SELECT cust_name, SUM(ttl_amt) AS total_spent
   FROM cust_tbl
   JOIN ord_tbls ON cust_tbl.cust_id = ord_tbls.cust_id
   WHERE ord_date >= DATEADD(month, -6, GETDATE())
   GROUP BY cust_name
   ord_tbl BY total_spent DESC
   LIMIT 5;
  1. What is the average order amount for customers who have purchased a “Laptop”?
    Ans.
   SELECT AVG(ttl_amt) AS average_ord_tbl_amount
   FROM ord_tbls
   WHERE cust_id IN (
       SELECT cust_id
       FROM ord_tbls
       WHERE prod_id = (
           SELECT prod_id
           FROM prod_tbl
           WHERE prod_name = 'Laptop'
       )
   );
  1. Which product has the highest profit margin?
    Ans.
   SELECT prod_name, (unit_price - cost_price) AS profit_margin
   FROM prod_tbl
   ord_tbl BY profit_margin DESC
   LIMIT 1;
  1. What is the percentage of customers who have placed more than one order?
    Ans.
   SELECT (COUNT(DISTINCT cust_id) / (SELECT COUNT(DISTINCT cust_id) FROM ord_tbls)) * 100 AS percentage
   FROM ord_tbls
   WHERE cust_id IN (
       SELECT cust_id
       FROM ord_tbls
       GROUP BY cust_id
       HAVING COUNT(*) > 1
   );

Must Read: 25 SQL Performance Interview Questions and Answers

Advanced Level

Certainly, here are the Advanced Level questions for the E-Commerce Store Database:

  1. Retrieve the customer name who spent the most on one single order.
    Ans.
   SELECT cust_name
   FROM cust_tbl
   WHERE cust_id = (
       SELECT cust_id
       FROM ord_tbls
       ord_tbl BY ttl_amt DESC
       LIMIT 1
   );
  1. List the products not ordered by any customer.
    Ans.
   SELECT prod_name
   FROM prod_tbl
   WHERE prod_id NOT IN (SELECT DISTINCT prod_id FROM ord_tbls);
  1. Calculate the average total amount spent by customers in May 2022.
    Ans.
   SELECT AVG(ttl_amt) AS average_ttl_amt
   FROM ord_tbls
   WHERE ord_date >= '2022-05-01' AND ord_date < '2022-06-01';
  1. Find the customers who have not placed any orders in May 2022.
    Ans.
   SELECT cust_name
   FROM cust_tbl
   WHERE cust_id NOT IN (
       SELECT DISTINCT cust_id
       FROM ord_tbls
       WHERE ord_date >= '2022-05-01' AND ord_date < '2022-06-01'
   );
  1. Get the names of all customers, along with the total amount they have spent, ordered by the total amount spent from highest to lowest.
    Ans.
   SELECT cust_name, SUM(ttl_amt) AS Total_Spend
   FROM cust_tbl
   JOIN ord_tbls ON cust_tbl.cust_id = ord_tbls.cust_id
   GROUP BY cust_name
   ord_tbl BY total_spent DESC;
  1. Find the customers who have placed orders in April and May 2022 but have not purchased a “Laptop.”
    Ans.
   SELECT cust_name
   FROM cust_tbl
   WHERE cust_id IN (
       SELECT cust_id
       FROM ord_tbls
       WHERE ord_date >= '2022-04-01' and ord_date < '2022-05-01'
       INTERSECT
       SELECT cust_id
       FROM ord_tbls
       WHERE ord_date >= '2022-05-01' and ord_date < '2022-06-01'
       EXCEPT
       SELECT cust_id
       FROM ord_tbls
       WHERE prod_id = (
           SELECT prod_id
           FROM prod_tbl
           WHERE prod_name = 'Laptop'
       )
   );
  1. Design an SQL trigger that updates the product inventory level whenever an order is placed.
    Ans.
   CREATE TRIGGER UpdateInventory
   ON ord_tbls
   AFTER INSERT
   AS
   BEGIN
       UPDATE prod_tbl
       SET inventory = inventory - (SELECT COUNT(*) FROM inserted WHERE inserted.prod_id = prod_tbl.prod_id)
       WHERE prod_id IN (SELECT prod_id FROM inserted);
   END;

These were the Advanced Level questions for your E-Commerce Store Database. If you have any further questions or need additional clarification, don’t hesitate to ask.

Summary

The above SQL questions cover a wide range of skills, from beginner to advanced levels, making it a comprehensive test to check your SQL programming skills. The use of an E-Commerce store database was to offer you a real scenario. Whether you’re a beginner or experienced, these questions give you a perfect chance to show your SQL knowledge.

Top SQL Queries Asked in Interviews

Check out the 50 most-asked SQL query interview questions.

50 QuestionsSQL Queries for Practice

We need your support to run this blog, so share this post on your social media accounts like Facebook / Twitter. This is how you will encourage us to come up with more informative stuff.

Happy Learning SQL,
Team TechBeamers.

You Might Also Like

MySQL vs MongoDB Comparison

How to Use Union in SQL Queries

IF Statement in SQL Queries: A Quick Guide

WHERE Clause in SQL: A Practical Guide

A Beginner’s Guide to SQL Joins

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article 1-10 Random Number Generator using JavaScript 1-10 Random Number Generator
Next Article 20 Pattern Programs in Python The Best 20 Python Programs to Print Patterns

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x