INNER JOIN
Joining two tables
To use data from both tables at once, you join them. An INNER JOIN pairs up rows that match on a key — here, the order's customer_id with the customer's id:
When a column name could come from either table, write table.column so it is clear.
ربط جدولين
لاستخدام بيانات من كلا الجدولين معًا، تقوم بـ ربطهما. يقوم INNER JOIN بمطابقة الصفوف المتطابقة بناءً على مفتاح — هنا، مفتاح الطلب customer_id مع مفتاح العميل id:
SELECT customer.name, orders.total
FROM customer
INNER JOIN orders ON customer.id = orders.customer_id;
عندما قد يأتي اسم عمود من أي جدول، اكتب table.column ليكون الأمر واضحًا.
Only matching rows survive
An INNER JOIN keeps a row only when the ON condition finds a match in the other table.
In our shop, Ben and Dan have no orders, so they do not appear in the join — there is nothing to pair them with. Ada and Cara, who do have orders, appear once for each of their orders.
فقط الصفوف المطابقة تنجو
يحافظ INNER JOIN على الصف فقط عندما يجد شرط ON تطابقًا في الجدول الآخر.
في متجرنا، بن ودان ليس لديهما طلبات، لذا لا يظهران في الربط — لا يوجد ما لدمجهما معه. أما آدا وكارا اللتان لديهما طلبات، فتظهران مرة واحدة لكل طلب من طلباتهن.
Common mistakes
- Match on the key with
ON; a missingONpairs every row with every row. - INNER JOIN drops rows with no match on the other side.
أخطاء شائعة
- مطابقة على المفتاح
ON؛ أيONمفقود يربط كل صف بكل الصفوف. - INNER JOIN يحذف الصفوف التي لا يوجد لها تطابق من الجانب الآخر.
INNER JOIN matches keys · يقوم INNER JOIN بمطابقة المفاتيح
Rows from two tables combine where the key matches. · تندمج صفوف من جدولين حيث يتطابق المفتاح.
List each customer's name next to the total of each of their orders. Join customer to orders on the key, ordered by orders.id. · اذكر name كل عميل بجانب total كل طلب من طلباته. اربط customer بـ orders عبر المفتاح، مرتباً حسب orders.id.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Show the customer name and order total for orders of 25 or more, largest total first. · أظهر name للعميل و total للطلب للطلبات التي تساوي 25 أو أكثر، الأكبر مبالغته أولاً.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.