SQL LESSON 31 OF 32

Keys, normalization & good design

A primary key identifies a row; a foreign key links to another table. Normalization reduces duplicated facts. Keep customer details in customers and reference customer_id from orders. Check performance and real requirements before redesigning.

हिन्दी में समझें

प्राथमिक कुंजी पंक्ति पहचानती है; विदेशी कुंजी दूसरी टेबल जोड़ती है। नॉर्मलाइज़ेशन दोहराई जानकारी घटाता है। ग्राहक जानकारी customers और संदर्भ orders में रखें।

अभ्यास: हर ग्राहक id, name और orders की संख्या total दिखाएँ, शून्य order वाले भी; id क्रम।

Hinglish mein samjhein

Primary key row identify karti hai; foreign key doosri table link karti hai. Normalization repeated facts kam karta hai. Customer details customers mein, customer_id orders mein rakho.

Practice: Har customer id,name aur order count total dikhao, zero-order bhi; customer id sorted.

A small SQL example

SELECT o.id,c.name,c.city FROM orders o JOIN customers c ON c.id=o.customer_id ORDER BY o.id;

Examples use the fictional shop database bundled with the SQL playground: customers, products, orders, order_items and employees. The engine is SQLite; other SQL dialects may differ.

Try this example in the SQL playground →

Practise this idea

Show each customer id, name and number of orders as total, including zero-order customers. Order by customer id.

Check answer & explanation
SELECT c.id,c.name,COUNT(o.id) AS total FROM customers c LEFT JOIN orders o ON c.id=o.customer_id GROUP BY c.id,c.name ORDER BY c.id;

LEFT JOIN keeps customers who have no orders. COUNT(o.id) excludes their unmatched NULL and returns zero rather than incorrectly counting one joined row.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

A foreign key…

  1. Links related tables
  2. Sorts every query
  3. Replaces all indexes
Check the quiz answer

Links related tables

A primary key identifies a row; a foreign key links to another table. Normalization reduces duplicated facts. Keep customer details in customers and reference customer_id from orders. Check performance and real requirements before redesigning.

References & corrections

Use the official SQLite documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.