Home > Database > Mysql Tutorial > How to Select Only the First 10 Rows in SQL Queries (MySQL and SQL Server)?

How to Select Only the First 10 Rows in SQL Queries (MySQL and SQL Server)?

Susan Sarandon
Release: 2025-01-24 19:57:11
Original
322 people have browsed it

How to Select Only the First 10 Rows in SQL Queries (MySQL and SQL Server)?

In the SQL query, only the first 10 lines

When processing large datasets, display all results may not be necessary or ideal. Limit the number of results displayed usually helps optimize performance and improve readability. This can be achieved through a simple and effective technology, which is called line restrictions.

In SQL Server, the line restrictions are executed using the "TOP" clause. By specifying a number (for example, 10) at the beginning of the inquiry, you can indicate that the database only retrieves the number of the number.

For example, to modify the query provided to display only the first 10 results:

In MySQL, the restrictions are implemented using "Limit" clauses. You do not need to use "TOP", but attach "Limit" to the end of the number of rows (eg, 10) to the end of the query.
<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

For MySQL, the query after modification is shown below:

The above is the detailed content of How to Select Only the First 10 Rows in SQL Queries (MySQL and SQL Server)?. 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