Posts

SQL FETCH

Image
Skip to content SQL FETCH Summary : in this tutorial, you will learn how to use the SQL   FETCH   clause to limit the number of rows returned by a query. Introduction to SQL FETCH clause To limit the number of rows returned by a query, you use the  LIMIT  clause. The  LIMIT  clause is widely supported by many database systems such as MySQL, H2, and HSQLDB. However, the  LIMIT  clause is not in SQL standard. SQL:2008 introduced the  OFFSET FETCH  clause which has a similar function to the  LIMIT  clause. The  OFFSET FETCH  clause allows you to skip  N  first rows in a result set before starting to return any rows. The following shows the syntax of the SQL  FETCH  clause: OFFSET offset_rows { ROW | ROWS } FETCH { FIRST | NEXT } [ fetch_rows ] { ROW | ROWS } ONLY Code language: SQL (Structured Query Language) ( sql ) In this syntax: The  ROW  and  ROWS ,  FIRST  and  NEXT  are the synonyms. Therefore, you can use them interchangeably. The  offset_rows  is an integer number which must be

SQL LIMIT

Image
Skip to SQL LIMIT Summary : in this tutorial, you’ll learn to use the SQL  LIMIT  clause to limit the number of rows returned from a query. Introduction to SQL LIMIT clause To limit the number of rows returned by a  select  statement, you use the  LIMIT  and  OFFSET  clauses. The following shows the syntax of  LIMIT  &  OFFSET  clauses: SELECT column_list FROM table1 ORDER BY column_list LIMIT row_count OFFSET offset ; Code language: SQL (Structured Query Language) ( sql ) In this syntax: The  LIMIT row_count  determines the number of rows ( row_count ) returned by the query. The  OFFSET offset  clause skips the  offset  rows before beginning to return the rows. The  OFFSET  clause is optional. If you omit it, the query will return the row_count rows from the first row returned by the  SELECT  clause. When you use the  LIMIT  clause, it is important to use an  ORDER BY  clause to ensure the order of rows in the result set. Not all database systems support the  LIM

SQL DISTINCT

Image
Skip to conten SQL DISTINCT Summary : in this tutorial, you will learn how to use the SQL  DISTINCT  operator to remove duplicates from a result set Introduction to SQL  DISTINCT  operator To remove duplicate rows from a result set, you use the  DISTINCT  operator in the  SELECT  clause as follows: SELECT DISTINCT column1, column2, ... FROM table1; Code language: SQL (Structured Query Language) ( sql ) If you use one column after the  DISTINCT  operator, the  DISTINCT  operator uses values in that column to evaluate duplicates. If you use two or more columns, the  DISTINCT  will use the combination of values in those columns to evaluate the duplicate. Note that the  DISTINCT  only removes the duplicate rows from the result set. It doesn’t delete duplicate rows in the table. If you want to select two columns and remove duplicates in one column, you should use the  GROUP BY  clause instead. SQL  DISTINCT  examples We will use the  employees  table in the  sample database  to