كل المقررات

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

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

Defining tables and querying one table

على طول: كتابة SQL: إنشاء جداول، إدخال داتا، وSELECT بالشروط والترتيب والتجميع.

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

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

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

رسوم متحركة

الـ SELECT والـ WHERE

أمثلة SQL

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

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

المثال ١: WHERE وـ ORDER BY

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

المثال ٢: UPDATE وـ DELETE

Sample output (press Run to produce your own):
name  | salary
------+-------
Mona  | 9000  
Omar  | 8000  
Nour  | 7000  
Salma | 6500  
(4 rows)

المثال ٣: GROUP BY وـ HAVING

Sample output (press Run to produce your own):
dept  | n | avg_salary
------+---+-----------
IT    | 2 | 8500.0    
Sales | 2 | 6750.0    
(2 rows)

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

اختبار قصير

اختبر فهمك

1. Which clause filters groups after aggregation?

2. How do you test for a missing value?

3. UPDATE without WHERE changes:

4. SQL is called declarative because:

5. DISTINCT does what?

تمارين برمجة

جرّب بإيدك

التمرين 1

اعرض أسماء موظفي Sales بس، مرتبين أبجدياً.

Show only the names of Sales employees, in alphabetical order.

(output appears here)

التمرين 2

اعرض مجموع المرتبات لكل قسم.

Show the total salary of each department.

(output appears here)

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

تقارير مبيعات من جدول واحد

الداتا: Orders sample data: 8 orders. 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. الجدول orders جاهز في الـ starter.
  2. سؤال ١: إجمالي المبيعات لكل عميل مرتب من الأكبر. المتوقع: Mona 1200 و Omar 700 و Nour 450.
  3. سؤال ٢: العملاء اللي إجمالي مشترياتهم أكبر من 500. المتوقع: Mona و Omar.
  4. سؤال ٣: متوسط قيمة الطلب. المتوقع: 293.75.
Expected output (press Run to produce your own):
customer | total
---------+------
Mona     | 1200 
Omar     | 700  
Nour     | 450  
(3 rows)
customer
--------
Mona    
Omar    
(2 rows)
avg_amount
----------
293.75    
(1 row)

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.