SQL Limit Clause
PLEASE NOTE: This tutorial comes from my FREE video course called SQL Boot Camp.
-- drop table posts;
create table posts (
id integer,
title character varying(100),
content text,
published_at timestamp without time zone
);
insert into posts (id, title, content, published_at) values (100, 'Intro to SQL', 'Epic SQL Content', '2018-01-01');
insert into posts (id, title, content, published_at) values (101, 'Intro to PostgreSQL', 'PostgreSQL is awesome!', now());
insert into posts (id, title, content) values (102, 'Intro to SQL Where Clause', 'Easy as pie!');
insert into posts (id, title, content) values (103, 'Intro to SQL Order By Clause', 'What comes first?');
select * from posts;
-- Postgres and some other DBs can use limit
select * from posts limit 1;
-- ANSI SQL standard
select * from posts fetch first 2 rows only;
-- Putting it all together
select *
from posts
where published_at is null
order by title
limit 1;
select *
from posts
where published_at is null
order by title desc
limit 1;
Please go ahead and leave a comment below if you have any questions about this tutorial.