Lecture 4
Lecture 4
INSERT INTO
links (url, name)
VALUES
('https://www.google.com','Google'),
('https://www.yahoo.com','Yahoo'),
('https://www.bing.com','Bing');
PostgreSQL UPDATE
The PostgreSQL UPDATE statement allows you to modify data in a table.
The following illustrates the syntax of the UPDATE statement:
UPDATE table_name
SET column1 = value1,
column2 = value2,
…
WHERE condition;
PostgreSQL UPDATE examples
DROP TABLE IF EXISTS courses;
CREATE TABLE courses (
course_id serial primary key,
course_name VARCHAR(255) NOT NULL,
description VARCHAR(500),
published_date date
);
INSERT INTO
courses(course_name, description, published_date)
VALUES
('PostgreSQL for Developers','A complete PostgreSQL for Developers','2020-07-13'),
('PostgreSQL Admininstration','A PostgreSQL Guide for DBA',NULL), 'PostgreSQL High
Performance',NULL,NULL),
('PostgreSQL Bootcamp','Learn PostgreSQL via Bootcamp','2013-07-11'),
('Mastering PostgreSQL','Mastering PostgreSQL in 21 Days','2012-06-30');
PostgreSQL UPDATE – updating one row
UPDATE courses
SET published_date = ’2020-08-01’
WHERE course_id = 3;
PostgreSQL DELETE
The PostgreSQL DELETE statement allows you to delete one or
more rows from a table. The following shows basic syntax of the
DELETE statement:
INSERT INTO
links
VALUES
('1', 'https://www.postgresqltutorial.com', 'PostgreSQL Tutorial', 'Learn
PostgreSQL fast and easy' , '2013-06-02'),
('2', 'http://www.oreilly.com', 'O''Reilly Media', 'O''Reilly Media', '2013-06-02'),
('3', 'http://www.google.com', 'Google', 'Google' , '2013-06-02'),
('4', 'http://www.yahoo.com', 'Yahoo', 'Yahoo' , '2013-06-02'),
('5', 'http://www.bing.com', 'Bing', 'Bing' , '2013-06-02'),
('6', 'http://www.facebook.com', 'Facebook', 'Facebook' , '2013-06-01'),
('7', 'https://www.tumblr.com/', 'Tumblr', 'Tumblr' , '2013-06-02'),
('8', 'http://www.postgresql.org', 'PostgreSQL', 'PostgreSQL', '2013-06-02');
1) Using PostgreSQL DELETE to delete one row from
the table:
Let’s start with the basic form of the SELECT statement that retrieves data from a single
table. The following illustrates the syntax of the SELECT statement:
SELECT
select_list
FROM
table_name;
PostgreSQL SELECT examples
1) Using PostgreSQL SELECT statement to query data from one column example:
2) Using PostgreSQL SELECT statement to query data from multiple columns example:
SELECT
first_name,
last_name,
email
FROM
customer;
3) Using PostgreSQL SELECT statement to query data from all columns of a table example:
SELECT
first_name || ‘ ’ || last_name,
email
FROM
customer;
SELECT 5 * 3;
SELECT now();
PostgreSQL Column Alias
A column alias allows you to assign a column or an expression in the
select list of a SELECT statement a temporary name. The column alias
exists temporarily during the execution of the query.
The following illustrates the syntax of using a column alias:
SELECT
first_name,
last_name AS surname
FROM
customer;
SELECT
first_name || ' ' || last_name AS full_name
FROM
customer;
PostgreSQL Table Aliases
Table aliases temporarily assign tables new names during the
execution of a query.
table_name AS alias_name;
PostgreSQL ORDER BY
The ORDER BY clause allows you to sort rows returned by a SELECT
clause in ascending or descending order based on a sort expression.
The following illustrates the syntax of the ORDER BY clause:
SELECT
select_list
FROM
table_name
ORDER BY
sort_expression1 [ASC | DESC],
…
sort_expressionN [ASC | DESC];
PostgreSQL ORDER BY examples
1) Using PostgreSQL ORDER BY clause to sort rows by one column:
SELECT
first_name,
last_name
FROM
customer
ORDER BY
first_name ASC;
2) Using PostgreSQL ORDER BY clause to sort rows by multiple columns:
SELECT
first_name,
last_name
FROM
customer
ORDER BY
first_name ASC,
last_name DESC;
PostgreSQL ORDER BY examples
The LENGTH() function accepts a string and returns the length of that string.
The following statement selects the first names and their lengths. It sorts the rows by the
lengths of the first names:
SELECT
first_name,
LENGTH(last_name) as len
FROM
customer
ORDER BY
len DESC;
PostgreSQL SELECT DISTINCT
The DISTINCT clause is used in the SELECT statement to
remove duplicate rows from a result set.
The following illustrates the syntax of the clause:
SELECT
DISTINCT column1
FROM
table_name;
PostgreSQL SELECT DISTINCT examples
CREATE TABLE distinct_demo (
id serial NOT NULL PRIMARY KEY,
bcolor VARCHAR,
fcolor VARCHAR
);
SELECT
DISTINCT bcolor
FROM
distinct_demo
ORDER BY
bcolor;
PART III. String Functions. Filtering Data.
Conditional Expressions & Operators.
PostgreSQL String Functions
Introduction to PostgreSQL CONCAT function
PostgreSQL introduced a built-in string function named CONCAT
to concatenate two or more strings into one.
2) SELECT
CONCAT (first_name, ' ', last_name) AS "Full name”
FROM
customer
3) SELECT
first_name,
CONCAT (‘Your first name ‘, length(first_name), ‘ characters’)
FROM
customer
PostgreSQL FORMAT Function
PostgreSQL FORMAT() function formats arguments based on a format
string.
2) SELECT
FORMAT('%s, %s’, last_name, first_name) full_name
FROM
customer
ORDER BY full_name;
PostgreSQL Letter Case Functions
• PostgreSQL LOWER function
• PostgreSQL UPPER function
• PostgreSQL INITCAP function
Examples
1) LOWER() FUNCTION
SELECT
LOWER(last_name)
FROM
customer
ORDER BY last_name;
2) UPPER() FUNCTION
SELECT
UPPER(last_name)
FROM
customer
ORDER BY last_name;
3)INITCAP() FUNCTION
SELECT
INITCAP(CONCAT (first_name, ‘ ’, last_name))
FROM
customer
ORDER BY first_name;
PostgreSQL LEFT Function
The PostgreSQL LEFT() function returns the first n characters in
the string.
2)SELECT LEFT('ABC',2);
3)SELECT LEFT('ABC',-2);
PostgreSQL RIGHT Function
The PostgreSQL RIGHT() function returns the last n characters in
a string.
3) SELECT last_name
FROM customer
WHERE RIGHT(last_name,3) = ‘son’;
PostgreSQL TRIM Function
• The LTRIM() function removes all characters, spaces by
default, from the beginning of a string.
• The RTRIM() function removes all characters, spaces by
default, from the end of a string.
• The BTRIM() function is the combination of the LTRIM() and
RTRIM() functions.
Examples
1)SELECT
LTRIM('enterprise', 'e’);
2)SELECT
RTRIM('enterprise', 'e’);
3)SELECT
BTRIM('enterprise', 'e');
PostgreSQL POSITION Function
The PostgreSQL POSITION() function returns the location of a
substring in a string.
POSITION(substring in string)
Example
1) SELECT POSITION('Tutorial' IN 'PostgreSQL Tutorial’);
2) UPDATE
customer
SET
email = REPLACE (
email,
‘qwerty1.org’,
‘postgressqltutorial.com’
);
PostgreSQL Substring Function
The substring function returns a part of string. The following
illustrates the syntax of the substring function:
2)SELECT
SUBSTRING ('PostgreSQL', 8); -- SQL
3) SELECT
SUBSTRING ('PostgreSQL' FROM 1 FOR 8); -- PostgreS
PostgreSQL Filtering Data
The SELECT statement returns all rows from one or more columns in a
table. To select rows that satisfy a specified condition, you use a WHERE
clause.