SQL beginner course · Chapter 4 of 10
SQL Operators and Patterns: Arithmetic, Comparison, Logic and LIKE explained in simple steps. Examples use practice tables such as students and scores; adapt names to your database.
Arithmetic operators
Use +, -, * and / in numeric expressions. Be careful with division by zero and integer division rules, which vary by product.
Example
SELECT points, points + 5 AS bonus_points FROM scores;Expected result: Each score gains a calculated bonus column.
Practice: Predict the result, run the query on a practice database, then change one part and explain what changed. For write commands, use disposable data.
Comparison operators
= checks equality; <> means not equal; <, <=, > and >= compare order. A NULL value is unknown, so compare it with IS NULL.
Example
SELECT student_id FROM scores WHERE points <> 0;Expected result: Rows with a nonzero known score appear.
Practice: Predict the result, run the query on a practice database, then change one part and explain what changed. For write commands, use disposable data.
AND, OR and NOT
AND requires both conditions, OR accepts either, and NOT reverses a condition. Parentheses make mixed conditions unambiguous.
Example
SELECT student_name FROM students WHERE grade >= 8 AND (active = 1 OR scholarship = 1);Expected result: Students meeting the grade and one of the other conditions appear.
Practice: Predict the result, run the query on a practice database, then change one part and explain what changed. For write commands, use disposable data.
IN, BETWEEN and LIKE
IN tests a list; BETWEEN includes both endpoints; LIKE matches text patterns. % means any number of characters and _ means one character in common SQL dialects.
Example
SELECT student_name FROM students WHERE student_name LIKE 'A%';Expected result: Names starting with A appear.
Practice: Predict the result, run the query on a practice database, then change one part and explain what changed. For write commands, use disposable data.
Unary and vendor-specific operators
Unary minus negates a number. Bitwise and assignment operators differ between databases; read the manual before using them in portable SQL.
Example
SELECT -points AS negative_points FROM scores;Expected result: The sign of each numeric score is reversed.
Practice: Predict the result, run the query on a practice database, then change one part and explain what changed. For write commands, use disposable data.
Quick review
Explain each concept in your own words. Recreate one example without looking at the code. Compare your result with the expected result above.
Browse the full SQL learning path. This is original teaching material; the syllabus was checked against the w3resource SQL tutorial index.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.