KOSUNI KOREA
Korean News in English
Technology

[MySQL Tutorial] Finding Specific Data with WHERE, ORDER BY, and LIMIT

Published October 17, 2026 · 1 views
[MySQL Tutorial] Finding Specific Data with WHERE, ORDER BY, and LIMIT

[Mysql 강좌] 7강 WHERE, ORDER BY, LIMIT로 원하는 데이터 찾기

You Rarely Need All Data

Last time, we learned how to use the SELECT statement to query data.

SELECT * FROM member;

This SQL retrieves all data from the member table.

However, in real websites, it's almost never necessary to display all data at once.

For example:

  • Finding a specific user

  • Viewing high-traffic posts

  • Retrieving only the latest 10 articles

  • Showing only posts by a specific author

These are much more common scenarios where you need to query specific data.


What is WHERE?

WHERE is a command for specifying conditions.

In simple terms, it's the feature that lets you retrieve only the data you want.

The basic syntax is as follows:

SELECT *
FROM member
WHERE id = 1;

This SQL queries only the user with ID 1.


Searching by Name

If you want to find a user named Hong Gildong, write it like this:

SELECT *
FROM member
WHERE name = 'Hong Gildong';

Strings must be enclosed in single quotes (').


Comparing Numbers

WHERE can use various comparison operators.

For example:

SELECT *
FROM board
WHERE hit > 100;

This retrieves posts with more than 100 views.


Common Comparison Operators

Operator

Meaning

=

Equal to

!=

Not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

These operators are frequently used in real-world applications.


Using Multiple Conditions

You can combine multiple conditions.

For example:

SELECT *
FROM board
WHERE writer = 'Admin'
AND hit > 100;

This queries posts written by the Admin with more than 100 views.


Using OR Conditions

OR means that only one of two conditions needs to be satisfied.

SELECT *
FROM member
WHERE name = 'Hong Gildong'
OR name = 'Kim Chul-soo';

This retrieves information for only those two users.


What is ORDER BY?

ORDER BY is a command for sorting data.

For example, in forums, it's common to display the newest posts first.

This is done using ORDER BY SQL.


Ascending Order Sorting

Ascending order sorts from smallest to largest values.

SELECT *
FROM member
ORDER BY id ASC;

ASC can be omitted as it's the default value.


Descending Order Sorting

Newest posts are typically displayed from highest to lowest number.

SELECT *
FROM board
ORDER BY id DESC;

DESC sorts from largest to smallest values.

This is the most commonly used sorting method in forums.


Sorting by Date

To get the newest posts, you can sort by date of creation.

SELECT *
FROM board
ORDER BY created_at DESC;

This outputs posts with the most recent creation dates first.


What is LIMIT?

LIMIT restricts the number of rows retrieved.

For example, to get only the latest 10 posts:

SELECT *
FROM board
LIMIT 10;

This is used for retrieving a specific number of rows.


Creating the First Page of a Forum

Forums typically display about 10 posts per page.

SELECT *
FROM board
ORDER BY id DESC
LIMIT 10;

This single SQL statement handles:

  • Displaying the newest posts

  • Sorting by number order

  • Outputting only 10 items

This is one of the most commonly used SQL statements in real-world applications.


Paging (Page Splitting)

LIMIT can also specify a starting position.

SELECT *
FROM board
LIMIT 0, 10;

This is the first page.

The second page would be:

SELECT *
FROM board
LIMIT 10, 10;

The third page would be:

SELECT *
FROM board
LIMIT 20, 10;

You can change the starting position to navigate through pages.

This is the core functionality of forum pagination.


Using WHERE + ORDER BY + LIMIT Together

The most common use case combines all three.

SELECT *
FROM board
WHERE writer = 'Admin'
ORDER BY id DESC
LIMIT 5;

This means:

  • Find only Admin's posts

  • Sort them in descending order

  • Display only 5 items

This structure is frequently used in real services.


User Search Example

To find a user with ID 'hong', write:

SELECT *
FROM member
WHERE userid = 'hong';

This is how user information is typically retrieved for login functionality.


E-commerce Example

To retrieve the top 20 most expensive items:

SELECT *
FROM product
ORDER BY price DESC
LIMIT 20;

This is a common method used in e-commerce product listings.


Common Mistakes for Beginners

1. Forgetting to use single quotes around strings

Incorrect example:

WHERE name = 홍길동

Correct example:

WHERE name = 'Hong Gildong'


2. Incorrect placement of ORDER BY

The correct order is always:

SELECT

↓

FROM

↓

WHERE

↓

ORDER BY

↓

LIMIT

Write in this sequence.


3. Placing LIMIT before ORDER BY

This is incorrect:

LIMIT

↓

ORDER BY

Always place LIMIT after ORDER BY.


4. Confusing ASC and DESC

  • ASC → Smallest to largest values

  • DESC → Largest to smallest values

For forums, DESC is most commonly used.


Key Takeaways from This Lesson

Important points to remember from this lesson:

  • WHERE retrieves data that meets specific conditions.

  • ORDER BY sorts the data.

  • ASC is ascending order (smallest to largest).

  • DESC is descending order (largest to smallest).

  • LIMIT restricts the number of rows retrieved.

  • The combination of all three is most common in real applications.

  • Forum listings typically use the WHERE + ORDER BY + LIMIT structure.

Mastering these three commands allows you to implement most data retrieval features used on websites.


In Closing

This lesson covered how to search for and sort specific data using WHERE, ORDER BY, and LIMIT.

You've now moved beyond simply retrieving all data to quickly fetching only the information you need.

Next time, we'll practice using UPDATE and DELETE to modify and remove data, learning core data management features for real applications.

FAQ

Why do I need single quotes around strings in SQL?

In SQL, strings must be enclosed in single quotes to distinguish them from other elements like column names or numbers. For example, 'Hong Gildong' is correct while Hong Gildong would cause an error.

What's the difference between ASC and DESC in ORDER BY?

ASC sorts data from smallest to largest values (ascending order), while DESC sorts from largest to smallest values (descending order). Most forums use DESC to show newest posts first.

Korean Source

This article is an English localization of a Korean-language report from BlogScore. Original: [Mysql 강좌] 7강 WHERE, ORDER BY, LIMIT로 원하는 데이터 찾기