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 employees
ORDER BY employee_id
LIMIT 5 OFFSET 10;

Output

Employee_IdFirst_NameLast_NameDate_Of_BirthDepartment
11JohnDoe1985-02-15HR
12MaryJohnson1987-04-25Finance
13JamesBrown1990-05-30Marketing
14LindaDavis1989-02-15IT
15RobertMiller1988-08-08sales

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 Employee
ORDER BY EmpID
OFFSET 5 ROWS
FETCH 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 employees
ORDER BY salary DESC
FETCH NEXT 5 ROWS ONLY;

Output

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY
----------- ----------- ----------- ----------
205 Shelley Higgins 12008
206 William Gietz 8300
114 Den Raphaely 11000
100 Steven King 24000
101 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.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.