-
Notifications
You must be signed in to change notification settings - Fork 1.1k
YSQL Tutorial: Fundamentals
In this tutorial you’ll learn how to use YugaByte DB’s PostgreSQL-compatible YSQL interface to query data from the Northwind sample database using SELECT, FROM, ORDER BY, LIMIT and other basic clauses and operators. For detailed information concerning specific clauses or operators, please refer to the official PostgreSQL documentation.
To start, make sure you have all the necessary prerequisites and have logged into the YSQL shell.
Let's begin!
SELECT data from one column
In this exercise we will select all the data in the company_names column from the customers table.
select company_name from customers;
The query should return 91 rows.
SELECT data from multiple columns
In this exercise we will select all the data in three columns from the employees table.
select employee_id,first_name,last_name,birth_date from employees;
The query should return 9 rows.
SELECT data from all the columns and rows in a table
In this exercise we will select all the data, from all columns in the order_details table.
select * from order_details;
The query should return 2155 rows.
SELECT with an expression
In this exercise we will combine the first_name and last_name columns to give us full names, along with their titles from the employees table.
select first_name || '' || last_name as full_name,title from employees;
The query should return 9 rows.
SELECT with an expression, but without a FROM clause
In this exercise we will use an expression, but omit specifying a table because it doesn't require one.
SELECT 5 * 3 AS result;
The query should return 1 row with a result of 15.
SELECT with a column alias
In this exercise we will use the alias title for the contact_title column and output all the rows.
SELECT contact_title AS title FROM customers;
The query should return 91 rows.
SELECT with a table alias
In this exercise we will use the alias details for the order_details table and output all the rows.
SELECT product_id, discount FROM order_details AS details;
The query should return 2155 rows.
SELECT with an ascending ORDER BY
In this exercise we sort employees by first_name in ascending order.
SELECT first_name,
last_name,
title,
address,
city
FROM employees
ORDER BY first_name ASC;
The query should return 9 rows.
SELECT with an descending ORDER BY
In this exercise we sort employees by first_name in descending order.
SELECT first_name,
last_name,
title,
address,
city
FROM employees
ORDER BY first_name DESC;
The query should return 9 rows.
SELECT with a ascending and descending ORDER BYs
In this exercise we sort employees by first_name in ascending order and then by last name in descending order.
SELECT first_name,
last_name
FROM employees
ORDER BY first_name ASC,
last_name DESC;
The query should return 9 rows
SELECT with DISTINCT on one column
Query
Results
SELECT with DISTINCT including multiple columns
Query
Results
SELECT with DISTINCT ON expression
Query
Results
WHERE clause with an equal = operator
Query
Results
WHERE clause with an AND operator
Query
Results
WHERE clause with an OR operator
Query
Results
WHERE clause with an IN operator
Query
Results
WHERE clause with a LIKE operator
Query
Results
WHERE clause with a BETWEEN operator
Query
Results
WHERE clause with a not equal <> operator
Query
Results
SELECT with a LIMIT clause
Query
Results
SELECT with LIMIT and OFFSET clauses
Query
Results
SELECT with LIMIT and ORDER BY clauses
Query
Results
SELECT with FETCH and ORDER BY clauses
In this exercise
Query
Results
SELECT with FETCH, OFFSET and ORDER BY clauses
In this exercise
Query
Results
SELECT with an IN clauses
Query
Results
SELECT with a NOT IN clause
Query
Results
SELECT with an IN clause in a subquery using CAST
Query
Results
SELECT with BETWEEN
Query
Results
SELECT with NOT BETWEEN
Query
Results
SELECT with a LIKE operator
Query
Results
SELECT with a LIKE operator using % and _
Query
Results
SELECT with a NOT LIKE operator
Query
Results
SELECT with an ILIKE operator
Query
Results
SELECT with a IS NULL operator
Query
Results
SELECT with a IS NOT NULL operator
Query
Results
Note: These instructions last tested with YugaByte DB 1.3
Like what you see? Don't forget to star us!