SQL for Data Analysis · Course lab · about 150 minutes · 6 tasks · marked out of 100, pass at 60
Answer a distributor's real questions from its sales tables
The situation
A fast-moving goods distributor in Faisalabad supplies shops across Punjab. Its director keeps asking questions the current spreadsheet cannot answer: which towns are growing, which products are slipping, which shops have stopped ordering. The data lives in four tables and nobody has written a query against them.
Build the queries that answer the director's real questions, prove each number, and leave queries the next analyst can read.
What you'll be able to show
- Write SELECT and WHERE knowing the order the database runs clauses
- Join tables without silently losing or multiplying rows
- Answer real questions with GROUP BY and HAVING and check the totals
- Structure a hard query as readable CTE steps and format it to last
What you need
- SQLite (DB Browser for SQLite), MySQL or PostgreSQL; any one is fine
- Four tables you create: shops, products, orders, order_lines, with invented but realistic data
Tasks
-
1Build the data
Create shops (id, name, town), products (id, name, category, price), orders (id, shop_id, order_date) and order_lines (order_id, product_id, quantity). Insert at least 50 orders across three months and six towns.A correct result: Row counts for each table and a note of the date range and towns covered. -
2The right clause
Write one query that returns the ten most recent orders from Gujranwala, newest first. Then say which clause each condition belonged in (WHERE vs HAVING) and why the LIMIT runs last.A correct result: The query, its result, and two sentences on clause order. -
3Join without losing rows
List every shop with its number of orders, including shops that have ordered nothing. Record the row count with an INNER JOIN and with a LEFT JOIN, and explain the difference.A correct result: Both counts, the correct query (LEFT), and the names of the shops the INNER dropped. -
4Answer the director
Answer three real questions with GROUP BY: revenue by town this quarter, the five products by quantity sold, and shops whose total orders are below a threshold (HAVING). Check one total against a plain SUM.A correct result: Three result sets and the reconciliation of one total. -
5Break it into steps
Find each town's best-selling product. Write it as named CTEs (for example town_product_qty, ranked) rather than nested subqueries.A correct result: A CTE query a colleague can read top to bottom, with no step named t1. -
6Make it last
Format the final three queries: one clause per line, uppercase keywords, indented joins. Replace any hardcoded date with a clear parameter or a comment, and add one comment explaining a business rule.A correct result: The formatted queries with the date handled and one business-rule comment.
What to hand in
One .sql file with the table creation, the data, every query in order and comments, plus a one-page answer to the director in plain words with the numbers.
How it is marked
| Criterion | Points |
|---|---|
| Data built and clause order explained correctly | 15 |
| Joins counted and the LEFT vs INNER difference explained | 25 |
| Three questions answered and one total reconciled | 25 |
| Hard query written as readable CTE steps | 15 |
| Queries formatted, dates handled, a business rule commented | 20 |
| Total · pass at 60 | 100 |
صورتحال
فیصل آباد کا ایک تیز فروخت اشیا کا ڈسٹری بیوٹر پنجاب بھر کی دکانوں کو مال دیتا ہے۔ اس کے ڈائریکٹر ایسے سوال پوچھتے رہتے ہیں جن کا جواب موجودہ اسپریڈشیٹ نہیں دے سکتی: کون سے قصبے بڑھ رہے ہیں، کون سی پروڈکٹس گر رہی ہیں، کن دکانوں نے آرڈر دینا بند کر دیا۔ ڈیٹا چار جدولوں میں ہے اور کسی نے ان پر کوئی کوئری نہیں لکھی۔
وہ کوئریاں بنائیں جو ڈائریکٹر کے اصل سوالوں کے جواب دیں، ہر عدد ثابت کریں، اور ایسی کوئریاں چھوڑیں جو اگلا تجزیہ کار پڑھ سکے۔
آپ کیا دکھا سکیں گے
- SELECT اور WHERE ایسے لکھنا کہ پتا ہو ڈیٹابیس clauses کس ترتیب میں چلاتا ہے
- جدولیں جوڑنا بغیر خاموشی سے قطاریں کھوئے یا بڑھائے
- GROUP BY اور HAVING سے اصل سوالوں کے جواب دینا اور مجموعے جانچنا
- مشکل کوئری کو پڑھنے لائق CTE قدموں میں بنانا اور دیرپا فارمیٹ کرنا
آپ کو کیا چاہیے
- SQLite (DB Browser for SQLite)، MySQL یا PostgreSQL؛ کوئی ایک کافی ہے
- چار جدولیں جو آپ بناتے ہیں: shops، products، orders، order_lines، فرضی مگر حقیقت پسندانہ ڈیٹا کے ساتھ
کام
-
1ڈیٹا بنائیں
shops (id, name, town)، products (id, name, category, price)، orders (id, shop_id, order_date) اور order_lines (order_id, product_id, quantity) بنائیں۔ تین مہینوں اور چھ قصبوں میں کم از کم 50 آرڈر ڈالیں۔درست نتیجہ: ہر جدول کی قطاروں کی تعداد اور تاریخ کی حد اور شامل قصبوں کا نوٹ۔ -
2درست clause
ایک کوئری لکھیں جو گوجرانوالہ کے دس تازہ ترین آرڈر، نئے پہلے، لوٹائے۔ پھر بتائیں ہر شرط کس clause میں تھی (WHERE یا HAVING) اور LIMIT آخر میں کیوں چلتا ہے۔درست نتیجہ: کوئری، اس کا نتیجہ، اور clause کی ترتیب پر دو جملے۔ -
3قطاریں کھوئے بغیر جوڑیں
ہر دکان اس کے آرڈروں کی تعداد کے ساتھ لکھیں، ان دکانوں سمیت جنہوں نے کچھ نہیں منگوایا۔ INNER JOIN اور LEFT JOIN سے قطاروں کی تعداد درج کریں، اور فرق بتائیں۔درست نتیجہ: دونوں تعداد، درست کوئری (LEFT)، اور ان دکانوں کے نام جو INNER نے چھوڑ دیں۔ -
4ڈائریکٹر کو جواب دیں
GROUP BY سے تین اصل سوالوں کے جواب دیں: اس سہ ماہی قصبے کے لحاظ سے آمدنی، مقدار کے لحاظ سے پانچ پروڈکٹس، اور وہ دکانیں جن کے کل آرڈر ایک حد سے کم ہیں (HAVING)۔ ایک مجموعہ سادہ SUM سے جانچیں۔درست نتیجہ: تین نتائج اور ایک مجموعے کی مطابقت۔ -
5قدموں میں بانٹیں
ہر قصبے کی سب سے زیادہ بکنے والی پروڈکٹ نکالیں۔ اسے nested subqueries کے بجائے نام والے CTEs (مثلاً town_product_qty، ranked) کے طور پر لکھیں۔درست نتیجہ: ایسی CTE کوئری جسے ساتھی اوپر سے نیچے پڑھ سکے، کوئی قدم t1 نام کا نہ ہو۔ -
6دیرپا بنائیں
آخری تینوں کوئریاں فارمیٹ کریں: ایک clause فی سطر، بڑے حروف میں کلیدی الفاظ، اندر ہٹے ہوئے joins۔ کسی ہارڈ کوڈ تاریخ کو واضح پیرامیٹر یا تبصرے سے بدلیں، اور ایک کاروباری اصول کی وضاحت کرتا تبصرہ لگائیں۔درست نتیجہ: فارمیٹ شدہ کوئریاں، تاریخ سنبھلی ہوئی، اور ایک کاروباری اصول کا تبصرہ۔
کیا جمع کروانا ہے
ایک .sql فائل جس میں جدولیں بنانا، ڈیٹا، ہر کوئری ترتیب سے اور تبصرے ہوں، ساتھ ڈائریکٹر کے لیے سادہ الفاظ میں ایک صفحے کا جواب اعداد کے ساتھ۔
نمبر کیسے ملیں گے
| معیار | نمبر |
|---|---|
| ڈیٹا بنا اور clause کی ترتیب درست سمجھائی گئی | 15 |
| joins گنے گئے اور LEFT بمقابلہ INNER کا فرق سمجھایا گیا | 25 |
| تین سوالوں کے جواب دیے اور ایک مجموعہ ملایا گیا | 25 |
| مشکل کوئری پڑھنے لائق CTE قدموں میں لکھی گئی | 15 |
| کوئریاں فارمیٹ ہوئیں، تاریخیں سنبھلیں، ایک کاروباری اصول پر تبصرہ | 20 |
| کل · پاس 60 | 100 |
Hand in your lab
Create a free BvLogic ID to hand in your lab, get it marked, and have it on your certificate.
Create your BvLogic IDAlready have one? Sign in and this course will be added to it.