SKIP
SKIP in SQL is a clause used to exclude a certain number of rows from the initial selection of results. It's commonly used in coordination with the LIMIT command to facilitate pagination in the retrieval of query results.
Example
SELECT *FROM employeesORDER BY employee_idLIMIT 5 OFFSET 10;
Output
Employee_Id | First_Name | Last_Name | Date_Of_Birth | Department |
---|---|---|---|---|
11 | John | Doe | 1985-02-15 | HR |
12 | Mary | Johnson | 1987-04-25 | Finance |
13 | James | Brown | 1990-05-30 | Marketing |
14 | Linda | Davis | 1989-02-15 | IT |
15 | Robert | Miller | 1988-08-08 | sales |
Explanation
In the given example, the OFFSET
keyword is used to skip the first 10 records in the employees
table. The LIMIT
keyword then selects the next 5 records. The ORDER BY
clause orders the records by employee_id
. So, this SQL query returns records from 11 to 15.
Example
SELECT *FROM EmployeeORDER BY EmpIDOFFSET 5 ROWSFETCH NEXT 10 ROWS ONLY;
Output
EmpID | Name | Rank--------+--------+-------- 6 | Tom | 6 7 | Bob | 7 8 | Alice | 8 9 | John | 9 10 | Laura | 10 11 | Sam | 11 12 | Steve | 12 13 | Chris | 13 14 | Sarah | 14 15 | Emma | 15
Explanation
This SQL Server specific statement sorts the ‘Employee’ table by ‘EmpID’ and skips the first 5 rows. After skipping, it fetches the next 10 rows. If the result set has less than 15 entries, fewer lines could be displayed.
Example
SELECT *FROM employeesORDER BY salary DESCFETCH NEXT 5 ROWS ONLY;
Output
EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY----------- ----------- ----------- ----------205 Shelley Higgins 12008206 William Gietz 8300114 Den Raphaely 11000100 Steven King 24000101 Neena Kochhar 17000
Explanation
The SELECT statement above orders the rows in the employees
table by salary
in descending order. It then uses the FETCH NEXT
clause to skip the top 5 rows, resulting in the 6th to 10th highest-paid employees being displayed.