Lesson 12 / الدرس 12

SELECT and WHERE / SELECT وWHERE

Asking a table for the rows you want and the columns you need. The syntax takes ten minutes; the part that takes longer is NULL, which does not behave the way your intuition insists it should.

سؤال جدول عن الصفوف التي تريدها والأعمدة التي تحتاجها. والصياغة تأخذ عشر دقائق؛ والذي يأخذ أطول هو NULL، وهو لا يتصرف كما يصر حدسك.

The shape of a query

SELECT   name, city          which columns
FROM     guests                which table
WHERE    city = 'Cairo'        which rows
ORDER BY name                  in what order
LIMIT    10;                   how many

written in that order, but the database reads FROM first, then WHERE,
then SELECT — which is why an alias made in SELECT cannot be used in WHERE
Everything in this lesson is one of those five clauses. The demonstration works through the operators on the guest house, and the last three panels are about NULL — read those slowly, because they are where the surprises live.

NULL is not a value

NULL means "no value here", and SQL treats it as unknown rather than as a thing. Every comparison with an unknown produces unknown, not true and not false — and WHERE keeps a row only when its condition is exactly true. So phone <> '0100000001' silently drops the guests who have no phone at all: the database cannot say their number differs, because it does not know their number. And phone = NULL returns nothing ever, because unknown does not equal unknown. This is not a quirk of the practice engine; it is the SQL standard, and it is the same in MySQL, PostgreSQL and every other database.

You writeYou getBecause
phone = NULLNo rows, everUnknown is not equal to unknown
phone IS NULLThe rows with no phoneThe only question SQL will answer about NULL
phone <> 'x'The known phones that differThe unknown ones are dropped silently
phone <> 'x' OR phone IS NULLWhat you probably meantYou have to say it
COALESCE(phone, '') <> 'x'The same, in one conditionTurn the unknown into a value first
NOT IN (1, 2, NULL)No rows, everThe same trap wearing a different hat

The last row catches experienced people. NOT IN with a NULL anywhere in the list can never be true, because SQL cannot rule out an unknown — so a perfectly sensible-looking query returns nothing and no error.

A few small things that save time. ORDER BY can take several columns, and the second only breaks ties in the first. LIMIT without ORDER BY gives you an arbitrary ten rows, not the first ten — the database is free to return them in any order, and it changes as the table grows. LIKE uses % for any run of characters and _ for exactly one. And a leading % means the database cannot use an index, so LIKE '%cairo%' reads every row — fine for six guests, not for six million.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Why does WHERE phone <> '0100000001' miss the guests with no phone?

2. What does NOT IN (1, 2, NULL) return?

3. Why avoid SELECT * in application code?

Your task / مهمتك

Write twelve queries against the guest house, each answering a question you write out in English first. Between them they must use every operator in this lesson: comparison, AND, OR, NOT, LIKE, IN, BETWEEN, IS NULL, ORDER BY with two columns, and LIMIT. Include one pair that differs only in bracketing and returns different rows. Then demonstrate the NULL trap: a query that looks right, misses rows, and its corrected version — with the row counts printed for both.

اكتب اثني عشر استعلامًا على بيت الضيافة، يجيب كل واحد سؤالًا تكتبه بالإنجليزية أولًا. وعليها معًا أن تستخدم كل مؤثر في هذا الدرس: المقارنة وAND وOR وNOT وLIKE وIN وBETWEEN وIS NULL وORDER BY بعمودين وLIMIT. وضمّنها زوجًا يختلف بالأقواس فقط ويعيد صفوفًا مختلفة. ثم اعرض فخ NULL: استعلام يبدو صحيحًا ويفوّت صفوفًا، ونسخته المصححة — مع طباعة عدد الصفوف لكليهما.

  • Twelve queries, each with its English question اثنا عشر استعلامًا بسؤال إنجليزي لكل واحد
  • Every operator in the lesson used at least once كل مؤثر في الدرس مستخدم مرة على الأقل
  • A pair differing only in brackets, with different results زوج يختلف بالأقواس فقط بنتائج مختلفة
  • The NULL trap shown and corrected, with both counts فخ NULL معروضًا ومصححًا بالعددين
How do you want to submit? / كيف تريد التسليم؟