Data & AI

SQL Real Time Examples: Real-World Use Cases Explained

Clear SQL real time examples from shopping, banking, hospitals, food delivery, HR and colleges, with the business question, query, result and meaning.

SQL in real lifeBanking reportsOnline ordersHospital recordsSales dashboards
  • SQL
  • Data Analytics
  • Tutorial

Quick answer: SQL is used wherever a company stores data in tables. Real examples include finding top-selling products, spotting customers who stopped buying, flagging unusual bank payments, measuring delivery delays, reporting hospital visits, studying team pay and tracking student attendance. In each case, a person turns a business question into a query and uses the result to take action.

The best way to understand SQL is to see the question behind the query. These SQL real time examples begin with something a manager may ask, move to a small table, and end with a decision. "Real time" here means everyday work, not live streaming data.

All tables are small and made up, so that you can check each answer by hand. We ran every query in SQLite. They use common SQL that also works in MySQL 8 and PostgreSQL.

SQL use cases at a glance

Industry Business question SQL idea
Online shopping Which products earn the most? JOIN, SUM, GROUP BY
Subscriptions Who has stopped buying? LEFT JOIN, IS NULL
Banking Which payments look unusual? Average by customer in a CTE
Food delivery Which area is late most often? CASE inside an aggregate
Hospital How many visits does each department get? COUNT, LEFT JOIN
HR Who earns the most in each team? Window function
College Whose attendance is low? HAVING

If joins are new, read SQL joins explained first.

Example 1: online shopping and top products

The question: "Which products brought in the most money?"

products

product_id name price
1 Bag 800
2 Bottle 200
3 Lamp 500

order_items

order_id product_id quantity
101 1 2
102 2 5
103 1 1
104 3 2
SELECT p.name,
       SUM(oi.quantity) AS units,
       SUM(oi.quantity * p.price) AS revenue
FROM products p
JOIN order_items oi ON oi.product_id = p.product_id
GROUP BY p.product_id, p.name
ORDER BY revenue DESC;
name units revenue
Bag 3 2400
Bottle 5 1000
Lamp 2 1000

What it means: the bottle sells the most units, but the bag earns the most. The answer changes with the word "top". Always ask, "Top by units or by money?"

A real-life detail: prices change. A real store saves the price paid in each order row. Otherwise, a price update would change last year's revenue.

Example 2: subscriptions and customers who left

The question: "Which customers placed no order in November?" A marketing team may send them a message.

customers

customer_id name
1 Asha
2 Ravi
3 Meena

orders

order_id customer_id order_date
11 1 2026-10-05
12 1 2026-11-08
13 2 2026-10-20
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o
       ON o.customer_id = c.customer_id
      AND o.order_date >= '2026-11-01'
      AND o.order_date <  '2026-12-01'
WHERE o.order_id IS NULL;
customer_id name
2 Ravi
3 Meena

What it means: Ravi bought in October but not in November. Meena never bought anything. They need different messages, so an analyst would split them into two groups.

Common mistake: Putting the date conditions in WHERE. Customers with no November order have NULL there, so they would be removed. Keep the conditions in ON. Our list of common SQL mistakes has more of these traps.

Example 3: banking and unusual payments

The question: "Which payments are more than three times the customer's average?"

payments

payment_id customer_id amount
1 1 500
2 1 400
3 1 600
4 1 500
5 1 9000
6 2 8000
7 2 9000
WITH customer_average AS (
    SELECT customer_id, AVG(amount) AS avg_amount
    FROM payments
    GROUP BY customer_id
)
SELECT p.payment_id, p.customer_id, p.amount
FROM payments p
JOIN customer_average a ON a.customer_id = p.customer_id
WHERE p.amount > 3 * a.avg_amount;
payment_id customer_id amount
5 1 9000

What it means: both customers have a payment of 9000. It is unusual for customer 1, whose average is 2200. It is normal for customer 2, whose average is 8500. One fixed limit for all customers would miss this.

This query only finds a row to check. It does not prove fraud. Real bank systems use many more signals, such as place, device and time. Notice too that the big payment raises its own average, so a careful analyst would compare it with earlier payments only.

The WITH part is a CTE, a named step in a query. See SQL CTEs for more.

Example 4: food delivery and late orders

The question: "What percentage of orders is late in each area?" Late means more than 40 minutes.

deliveries

delivery_id area minutes
1 North 30
2 North 55
3 North 35
4 North 38
5 South 50
6 South 45
7 South 25
8 South 60
SELECT area,
       COUNT(*) AS orders,
       SUM(CASE WHEN minutes > 40 THEN 1 ELSE 0 END) AS late_orders,
       ROUND(100.0 * SUM(CASE WHEN minutes > 40 THEN 1 ELSE 0 END)
             / COUNT(*), 1) AS late_pct
FROM deliveries
GROUP BY area
ORDER BY late_pct DESC;
area orders late_orders late_pct
South 4 3 75.0
North 4 1 25.0

What it means: South has the bigger problem. The manager may check the number of riders, traffic or cooking time there. SQL shows where to look. People still need to find out why.

The CASE turns each late order into 1 and each on-time order into 0. Adding them gives a count. This trick is used in many reports.

Example 5: hospital visits by department

The question: "How many visits did each department get, including those with none?"

departments

dept_id dept_name
1 Heart
2 Eye
3 Skin

visits

visit_id dept_id patient_id
1 1 71
2 1 72
3 1 71
4 2 73
SELECT d.dept_name,
       COUNT(v.visit_id) AS visits,
       COUNT(DISTINCT v.patient_id) AS patients
FROM departments d
LEFT JOIN visits v ON v.dept_id = d.dept_id
GROUP BY d.dept_id, d.dept_name
ORDER BY d.dept_id;
dept_name visits patients
Heart 3 2
Eye 1 1
Skin 0 0

What it means: Heart had three visits from two patients, because patient 71 came twice. Skin shows 0 instead of disappearing. Using COUNT(*) here would wrongly show 1 for Skin, because the LEFT JOIN still creates one row.

Note: Health data is private. In real work, an analyst should see only the columns needed for the task. Names and phone numbers are often hidden.

Example 6: HR and the highest pay in each team

The question: "Who earns the most in each team?"

staff

emp_id name team salary
1 Kiran Tech 60000
2 Divya Tech 75000
3 Suresh Sales 40000
4 Latha Sales 40000
SELECT team, name, salary
FROM (
    SELECT team, name, salary,
           RANK() OVER (PARTITION BY team ORDER BY salary DESC) AS rnk
    FROM staff
) ranked
WHERE rnk = 1
ORDER BY team, name;
team name salary
Sales Latha 40000
Sales Suresh 40000
Tech Divya 75000

What it means: Sales has a tie, and RANK() shows both people. With ROW_NUMBER(), one would be dropped. Ask how ties should be handled before you send the report.

A window function ranks rows without joining them into one. Learn more in our window functions lesson.

Example 7: college attendance alerts

The question: "Which students are below 75 percent attendance?"

attendance (1 means present, 0 means absent)

student class_no present
Anil 1 1
Anil 2 1
Anil 3 1
Anil 4 0
Bindu 1 1
Bindu 2 0
Bindu 3 0
Bindu 4 1
SELECT student,
       ROUND(100.0 * SUM(present) / COUNT(*), 1) AS attendance_pct
FROM attendance
GROUP BY student
HAVING 100.0 * SUM(present) / COUNT(*) < 75;
student attendance_pct
Bindu 50.0

What it means: Bindu needs a call. Anil is at exactly 75, so he is not below 75. Small words such as "below" and "at least" change the sign in your query.

Interview note: Explain why HAVING is used here. Attendance percentage is known only after rows are grouped. WHERE runs before grouping, so it cannot use the total.

SQL in industry: from question to action

In every example above, the query was one small part. SQL in industry follows these steps:

  1. Ask: what decision will this answer help with?
  2. Define: what does "late", "active" or "top" mean?
  3. Find: which tables have the data?
  4. Write: build the query in small parts.
  5. Check: test totals and a few rows by hand.
  6. Explain: tell the team what you found and what to do.
  7. Repeat: put it in a dashboard if it is needed often.

A simple summary helps. Instead of "late_pct is 75", say: "Three out of four South orders took over 40 minutes, so let us check that area first." Remember to add the size of your data. Four orders are enough for a lesson, but not for a business decision.

Role How SQL is used in the job
Data analyst Reports, trends and answers for teams
Backend developer Saving and reading app data
Tester Checking if the app saved the right data
Support engineer Finding why one user has a problem
Data engineer Moving and cleaning large data

How to practise SQL real time examples

Use SQL real world tasks instead of only syntax drills. Pick a place you know, such as a canteen, library, gym or bus stand. Then do this:

  • draw three or four tables
  • add 20 rows, with a few missing values
  • write ten questions the owner may ask
  • answer them with SQL
  • check three answers by hand

Pro tip: Include one person with no orders and one tie for first place. These two cases catch many wrong queries.

For ready-made ideas, try our SQL projects. To prepare for placements, practise SQL interview questions for freshers.

SQL is one full phase of the Data Analytics with AI course, taught in Madhapur, Hyderabad and live online for students across Andhra Pradesh and Telangana. Our data analytics course guide shows how SQL fits with Excel, Python and Power BI.

Frequently asked questions

What is SQL used for in real life?

SQL is used to store, find, update and summarise data in relational databases. Apps use it to save orders and payments. Analysts use it to make reports and answer business questions.

Which companies use SQL?

Companies in banking, retail, healthcare, telecom, education, travel and software commonly use SQL because they keep records in databases. The job title may be analyst, developer, tester or support engineer.

Are these queries written for MySQL?

They use standard SQL features that also work in MySQL 8 and PostgreSQL. We ran them in SQLite. Date functions differ between databases, so these examples use simple date comparisons.

How can I practise SQL with real-world data?

Pick a small business such as a shop or library. Make three or four tables, add 20 rows, and write ten questions its owner may ask. Answer each with SQL and check a few by hand.

Is SQL alone enough for a data analyst job?

SQL is a core skill, but most analyst roles also need Excel, a dashboard tool, basic statistics and clear communication. Python is listed in many roles as well.


Want to solve problems like these with a trainer? Book a free demo class for the Data Analytics with AI course, at our Madhapur campus or live online.

Found this useful? Share it: LinkedIn Facebook WhatsApp

Written by

Siva Prasad Galaba

Staff Engineer & Founder, CodeBegun

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaching the next generation to code the way the industry actually works.

Back to all articles
Chat with us