background

Introduction to SQL

PLEASE NOTE: This tutorial comes from my FREE video course called SQL Boot Camp.

-- Connect using psql, execute a SELECT and create a test database

select 'Hello World!';

create database test;

-- Connect using psql to the test database
-- psql test

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!');

-- Retrieve all columns from all posts
select * from posts;

-- Retrieve specific columns
select title from posts;

select title as header from posts;

-- Use aggregate function
select count(*) from posts;

-- Filter rows returned 
select * from posts where id = 101;

-- Order rows returned
select * from posts order by title;

-- Putting it all together
select *
from posts
where published_at > '2018-02-01'
order by title;

Please go ahead and leave a comment below if you have any questions about this tutorial.

Sql introduction