SQL / MySQL Beginner Queries · 7 min read

SELECT, WHERE, and Comparison Predicates

Lesson 6 of 33 in the SQL / MySQL learning path.

How do I read specific rows from a MySQL table?

A registrar rarely wants every column of every student. SELECT names the columns, FROM names users, and WHERE keeps the rows that match a predicate. This lesson looks up one student by email and also limits created_at to a half-open range, so a timestamp on the last instant of the window is included and the next day is not. A second query uses only the date range and returns the students who registered in March 2026.

The recommended queries list id, name, email, and created_at. They do not use SELECT *, so password_hash stays out of the result. Comparison operators in the sample are equality and the range pair greater-than-or-equal and less-than. Application code should bind the email and the two timestamps with PDO placeholders, which is the pattern in php-pdo-prepared.

Syntax & Parameters

Syntax
SELECT column_list
FROM table_name
WHERE column_name = value
  AND created_at >= '2026-01-01 00:00:00'
  AND created_at < '2027-01-01 00:00:00';

Example

queries.sql
CREATE DATABASE IF NOT EXISTS school
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE school;

DROP TABLE IF EXISTS enrollments;
DROP TABLE IF EXISTS courses;
DROP TABLE IF EXISTS users;

CREATE TABLE users (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(120) NOT NULL,
  email VARCHAR(254) NOT NULL,
  password_hash VARCHAR(255) NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_users_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO users (name, email, password_hash, created_at) VALUES
('Ada Lovelace', 'ada@school.test', 'sample-hash-not-a-secret', '2026-01-15 09:00:00'),
('Grace Hopper', 'grace@school.test', 'sample-hash-not-a-secret', '2026-03-02 11:30:00'),
('Alan Turing', 'alan@school.test', 'sample-hash-not-a-secret', '2025-11-20 08:15:00'),
('Katherine Johnson', 'katherine@school.test', 'sample-hash-not-a-secret', '2026-03-18 14:00:00');

SELECT id, name, email, created_at
FROM users
WHERE email = 'ada@school.test'
  AND created_at >= '2026-01-01 00:00:00'
  AND created_at < '2027-01-01 00:00:00';

SELECT id, name, email, created_at
FROM users
WHERE created_at >= '2026-03-01 00:00:00'
  AND created_at < '2026-04-01 00:00:00'
ORDER BY created_at, id;

Expected Output

Email plus 2026 range
id | name         | email           | created_at
1  | Ada Lovelace | ada@school.test | 2026-01-15 09:00:00
1 row in set

March 2026 range
id | name              | email                  | created_at
2  | Grace Hopper      | grace@school.test      | 2026-03-02 11:30:00
4  | Katherine Johnson | katherine@school.test  | 2026-03-18 14:00:00
2 rows in set

Line-by-Line Walkthrough

  1. The script creates users and inserts four students with known created_at values so the comparisons are stable no matter when you run the file.
  2. The first SELECT lists id, name, email, and created_at. password_hash is omitted on purpose.
  3. email = ada@school.test is an equality predicate. The unique index can satisfy it.
  4. created_at >= 2026-01-01 00:00:00 AND created_at < 2027-01-01 00:00:00 is a half-open range. The start instant is included, and the first instant of 2027 is excluded.
  5. Ada matches both the email and the 2026 range. Alan registered in 2025, so the range rejects him even before the email test is considered.
  6. The second query drops the email test and keeps March 2026, from the first instant of March up to and not including the first instant of April.
  7. Grace registered on 2 March and Katherine on 18 March, so both rows return. Ada is in January and Alan is in 2025, so they are absent.
  8. ORDER BY created_at, id makes the March list stable if two students share a timestamp.
  9. The literals are for the mysql client. Application code should bind the email and the timestamps with PDO placeholders, as in php-pdo-prepared.
  10. A comparison with NULL uses IS NULL. The expression email = NULL is unknown and matches no rows, which matters later for optional columns such as a mentor.

Common Pitfalls & Security

A SELECT is still a data exposure if it returns secrets. Listing columns keeps password_hash off the page. WHERE is what makes the result a specific student or a specific month instead of the whole school. Build ranges on DATETIME with a start that is included and an end that is excluded, so the last moment of the month is included exactly once.

  • SELECT * returns every column, including password_hash, and it changes shape when a column is added. The queries in this lesson name the columns they need.
  • BETWEEN '2026-03-01' AND '2026-03-31' casts the end to midnight, so a registration later on 31 March is missed. The half-open form created_at >= start AND created_at < next_start covers the whole month.
  • email = NULL does not find missing emails. Use IS NULL. Under the usual collation, email equality is case-insensitive, so Ada@school.test matches ada@school.test.
  • Gluing a search box into the SQL text changes the statement. Bind the email and the dates with PDO placeholders, as php-pdo-prepared shows.
  • A filter with no matching row returns an empty set and is a successful query. Check for zero rows in the application before you assume the student exists.
  • An UPDATE or DELETE typed while you are practicing still needs WHERE. Omitting it changes every student, which is a catastrophe. These samples only read.

Best Practices & Modern Standards

Select the columns the screen needs, filter with equality on an indexed column such as email, and express date windows as a half-open range on created_at. Bind every value that comes from a form. The mysql script writes the literals so the result is easy to check. php-pdo-prepared is the application form of the same statement.

Sort a multi-row result with ORDER BY when the caller will display it. The next lesson adds LIMIT and OFFSET on top of an ordered read. Keep password_hash out of ordinary student lists, and keep the predicate sargable: a function wrapped around the column, such as DATE(created_at), blocks a plain index on that column.

Real-World Use

Staff look up one student in users by email when they answer a registration question, and they list students who joined during a term by filtering created_at. Courses and enrollments stay out of this query because the question is about the account itself. The same WHERE habits apply when the report later joins those tables: name the columns, compare the email exactly, and bound the timestamp with a start and an end that do not overlap the next day.

Frequently Asked Questions

Why list columns instead of selecting every column?

The column list returns id, name, email, and created_at, and it leaves password_hash in the table. A star would include the hash and would also change the result when someone adds a column. Name what the page needs.

Why is the date test written with two comparisons?

created_at >= start AND created_at < end includes every instant from the start up to but not including the end. That covers a whole month even when a student registers at 23:59. A date-only end value stops at midnight.

Does email matching care about capital letters?

Under utf8mb4_unicode_ci, Ada@school.test and ada@school.test compare as equal. The unique index uses the same rule. Store one canonical form if the school must treat those strings as different addresses.

How should a PHP page pass the email?

Prepare the SELECT and bind the email and the two timestamps. The SQL keeps the column list and the WHERE clause. php-pdo-prepared shows the binding. This lesson uses literals so you can see the exact rows MySQL returns.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…