كل المقررات

الرئيسية / مقرر قاعدة البيانات / الفصل 5 الجزء 2

الفصل 5 (الجزء 2): SQL

Joins, subqueries and views

على طول: ربط جداول بـ JOIN، الـ subqueries، والـ views.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

الـ JOIN

أمثلة SQL

أمثلة قصيرة وبسيطة

الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.

المثال ١: INNER JOIN

Sample output (press Run to produce your own):
name | label
-----+------
Mona | IT   
Nour | IT   
Omar | Sales
(3 rows)

المثال ٢: LEFT JOIN

Sample output (press Run to produce your own):
label | people
------+-------
HR    | 0     
IT    | 2     
Sales | 1     
(3 rows)

المثال ٣: Subquery وـ View

Sample output (press Run to produce your own):
name | salary
-----+-------
Mona | 9000  
Omar | 8000  
(2 rows)
name | salary
-----+-------
Mona | 9000  
Omar | 8000  
(2 rows)

افتح الموضوع المقابل في صفحة LiU TDDD12

اختبار قصير

اختبر فهمك

1. INNER JOIN drops rows that:

2. Which join keeps all rows of the left table?

3. A view stores:

4. A join with no ON condition gives:

5. EXISTS is true when the subquery:

تمارين برمجة

جرّب بإيدك

التمرين 1

اعرض الأقسام اللي مفيهاش موظفين (LEFT JOIN وـ IS NULL).

Show departments that have no employees (LEFT JOIN and IS NULL).

(output appears here)

التمرين 2

اعمل view اسمه high_paid فيه الموظفين اللي مرتبهم أكتر من 7000.

Create a view called high_paid with employees earning more than 7000.

(output appears here)

مشروع صغير في آخر المحاضرة

متجر: ثلاث جداول وتقارير

الداتا: Shop sample data: 3 customers, 3 products, 6 order lines. Written for this course (our own data, free to reuse).

License / source: Our own sample data; SQLite is in the public domain. Dataset page

الخطوات

  1. الجداول جاهزة في الـ starter: customer و product و line.
  2. سؤال ١ (join): اسم العميل واسم المنتج والكمية لكل سطر.
  3. سؤال ٢: إجمالي إنفاق كل عميل (الكمية × السعر). المتوقع: Mona 130 و Omar 40 و Nour 0 (استخدم LEFT JOIN).
  4. سؤال ٣: المنتجات اللي محدش اشتراها. المتوقع: Lamp.
Expected output (press Run to produce your own):
name | title | qty
-----+-------+----
Mona | Pen   | 5  
Mona | Book  | 2  
Mona | Pen   | 2  
Omar | Book  | 1  
Omar | Pen   | 1  
Mona | Pen   | 0  
(6 rows)
name | spent
-----+------
Mona | 130  
Nour | 0    
Omar | 40   
(3 rows)
title
-----
Lamp 
(1 row)

مشروع نهاية الفصل

ملف المشروع العملي للفصل 5

ملف PDF فيه مشروع صغير بأسئلة عملية تطبّق بيها كل اللي اتعلمته في الفصل، مع حل نموذجي في الآخر.

افتح ملف المشروع (PDF)

Credits: the topic order follows the lecture list of the course TDDD12 Database Technology at Linköping University (https://www.ida.liu.se/~TDDD12/fo/), whose reading guide is based on the textbook Fundamentals of Database Systems by Elmasri and Navathe. We found no reuse licence on that page, so we link to it and copy nothing from it: the explanations, diagrams, data and code in this lecture are our own. Reference: Elmasri and Navathe, Fundamentals of Database Systems, the chapters on SQL.