Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Monday, 19 February 2024

Limit Function in TERADATA

In Teradata, you can use the QUALIFY clause along with the ROW_NUMBER() function to achieve a similar effect as the LIMIT function in other database systems. Here's how you can use it with an example:

Suppose you have a table called employees with columns employee_id, first_name, and last_name, and you want to retrieve the first 5 employees ordered by employee_id:

SELECT employee_id, first_name, last_name

FROM employees

QUALIFY ROW_NUMBER() OVER (ORDER BY employee_id) <= 5;


In this query:

- SELECT employee_id, first_name, last_name specifies the columns you want to retrieve.

- FROM employees specifies the table from which to retrieve the data.

- QUALIFY ROW_NUMBER() OVER (ORDER BY employee_id) <= 5 assigns a row number to each row based on the ordering by employee_id and then filters only those rows where the row number is less than or equal to 5.

This query will return the first 5 rows from the employees table, sorted by employee_id.

If you also want to include additional columns, you can adjust the SELECT clause accordingly. Additionally, you can use the ORDER BY clause within the ROW_NUMBER() function to specify a different ordering if needed.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.