Home > Database > Mysql Tutorial > How to Select Only the First 10 Rows in SQL Queries?

How to Select Only the First 10 Rows in SQL Queries?

Barbara Streisand
Release: 2025-01-24 19:47:14
Original
421 people have browsed it

How to Select Only the First 10 Rows in SQL Queries?

Tricks to select the first 10 rows in SQL query

In SQL, there are mainly two ways to select only the first 10 rows in the query results. The exact method depends on the database management system (DBMS) used.

TOP keywords in SQL Server

For Microsoft SQL Server, you can use the TOP keyword to retrieve a specified number of rows from the beginning of the result set. For example:

<code class="language-sql">SELECT TOP 10 a.names, COUNT(b.post_title) AS num
FROM wp_celebnames a
JOIN wp_posts b ON INSTR(b.post_title, a.names) > 0
WHERE b.post_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
GROUP BY a.names
ORDER BY num DESC;</code>
Copy after login

LIMIT clause in MySQL

In MySQL, use the LIMIT clause to limit the number of rows returned by a query. It comes after the ORDER BY clause (if present) and specifies the maximum number of rows to be included in the result set. For example:

<code class="language-sql">SELECT a.names, COUNT(b.post_title) AS num
FROM wp_celebnames a
JOIN wp_posts b ON INSTR(b.post_title, a.names) > 0
WHERE b.post_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
GROUP BY a.names
ORDER BY num DESC
LIMIT 10;</code>
Copy after login

By using TOP or LIMIT, you can easily retrieve the first 10 rows of a specified query in SQL Server or MySQL respectively.

The above is the detailed content of How to Select Only the First 10 Rows in SQL Queries?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template