Support Online
Skip to main content

Using SQL WHERE: Data Filtering Guide

🚀 What Will You Learn in This Guide?

This guide teaches you the basics of filtering query results with the WHERE clause in SQL.
You will see how to determine which rows will be affected in SELECT, UPDATE and DELETE queries with search conditions.
You will also learn step by step to create more complex filters using logical operators such as AND, OR, and NOT.

🧠 Technical Summary

Subject: Row-based data filtering with WHERE clause in SQL
Purpose: To ensure that queries only process rows that meet certain criteria
Steps:

  1. Creating a sample database
  2. Filtering with simple WHERE conditions
  3. Using comparison operators
  4. Special expressions (IS NULL, BETWEEN, IN, LIKE)
  5. Condition concatenation with logical operators (AND, OR, NOT)
  6. Prioritization with parentheses

⚙️ 1. Sample Database Setup

Connect to MySQL
mysql -u genixnode_golfer -p

➡️ Logs into the MySQL command line.

Create the Database and Table
CREATE DATABASE golf_vt;
USE golf_vt;

➡️ Creates and activates a new test database.

CREATE TABLE golfculer (
isim VARCHAR(20),
oynanan_round INT,
en_iyi INT,
en_kotu INT,
ortalama DECIMAL(4,1),
galibiyet INT
);

➡️ Creates a table that records golfer performances.

Add Sample Data

INSERT INTO golfculer VALUES
('Can', 22, 68, 103, 84.6, 3),
('Pat', 25, 65, 74, 68.7, 9),
('Gözde', 11, 78, 118, 97.6, 0),
('Deniz', 23, 70, 92, 78.8, 1),
('Alp', NULL, 63, 76, 68.5, 7),
('Ayşe', NULL, 69, 84, 76.7, 4),
('Rüya', 18, 67, 92, 81.3, 1);

➡️ Seven golfers add their records to the table.


🎯 2. Basic WHERE Conditions

Syntax

SELECT sütunlar
FROM tablo
WHERE arama_koşulu;

➡️ Returns rows where the condition is true.

Equality (=)
SELECT isim, galibiyet
FROM golfculer
WHERE galibiyet = 1;

➡️ Returns golfers with exactly 1 win count.

Not Equal (<>)
SELECT isim, galibiyet
FROM golfculer
WHERE galibiyet <> 1;

➡️ Shows all records whose win count is not 1.

Comparisons
Kodu kopyala
SELECT isim, galibiyet
FROM golfculer
WHERE galibiyet > 1;

➡️ Returns rows with win count greater than 1.


🧩 3. Special Condition Operators

Null Value Check (IS NULL)
SELECT isim, oynanan_round
FROM golfculer
WHERE oynanan_round IS NULL;

➡️ finds golfers with unknown (NULL) round value.

Range Determination (BETWEEN)
SELECT isim, en_iyi
FROM golfculer
WHERE en_iyi BETWEEN 67 AND 73;

➡️ Lists golfers whose best scores are in the range of 67–73.

Cluster Membership (IN)
SELECT isim, en_iyi
FROM golfculer
WHERE en_iyi IN (65, 67, 69);

➡️ Returns rows whose best value is one of the values in this list.

Pattern Matching (LIKE)
SELECT isim
FROM golfculer
WHERE isim LIKE 'A%';

➡️ Finds golfers whose names start with the letter "A".


🔗 4. Combining Multiple Conditions

AND (AND)
SELECT isim, en_iyi, en_kotu
FROM golfculer
WHERE en_iyi < 70 AND en_kotu < 96;

➡️ Returns rows that satisfy both conditions.

OR (OR)
SELECT isim, en_iyi, en_kotu
FROM golfculer
WHERE en_iyi < 70 OR en_kotu < 96;

➡️ Returns rows that satisfy at least one of the conditions.

Prioritization (Parenthesis)
SELECT isim, ortalama, en_kotu, oynanan_round
FROM golfculer
WHERE (ortalama < 85 OR en_kotu < 95) AND oynanan_round BETWEEN 19 AND 23;

➡️ Parentheses determine which condition will run first.


🚫 5. Exclusion of Results (NOTE)

IS NOT NULL
SELECT isim, oynanan_round
FROM golfculer
WHERE oynanan_round IS NOT NULL;

➡️ Returns the rows whose played_round value is full.

NOT LIKE
SELECT isim
FROM golfculer
WHERE isim NOT LIKE 'R%';

➡️ Returns records whose names do not start with "R".

Grouped Exclusion
SELECT isim, ortalama, en_iyi, galibiyet
FROM golfculer
WHERE NOT (ortalama < 80 AND en_iyi < 70) OR galibiyet = 9;

➡️ Excludes rows that meet certain condition, but includes those with win = 9.


💬 Frequently Asked Questions (FAQ)

  1. Is WHERE used only with SELECT?

No. It also determines which rows will be affected in UPDATE and DELETE queries.

  1. What is the difference between WHERE and HAVING?

WHERE filters individual rows; HAVING filters the groups created after GROUP BY.

  1. Is LIKE case sensitive?

It is generally not available in MySQL, but it is sensitive in PostgreSQL.

  1. Why can't I compare NULL values ​​with =?

NULL represents unknown value. That's why IS NULL is used instead of =.

  1. What is the fastest condition in terms of performance?

Direct equality (=) queries on indexed columns are generally the fastest.


🧭 Result

In this guide, you learned data filtering techniques with the WHERE clause in SQL. You can now refine your queries using comparison, range, and logical operators.

For faster queries and strong database performance, you can try your projects on the GenixNode platform and set up your high-speed SQL infrastructure immediately 🚀