0% found this document useful (0 votes)
100 views25 pages

PostgreSQL Lab6

- UPDATE COMPANY SET SALARY = SALARY * 1.5 WHERE ID IN (SELECT ID FROM COMPANY_BKP WHERE SALARY < 50000); This updates the salary in the COMPANY table by multiplying it by 1.5 for all records that have an ID present in the COMPANY_BKP table and whose salary is less than 50000. The subquery (SELECT ID FROM COMPANY_BKP WHERE SALARY < 50000) is used to filter the records in COMPANY table based on a condition on the COMPANY_BKP table. Only those records from COMPANY table are updated whose ID satisfies the condition in the subquery.

Uploaded by

Java Tech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views25 pages

PostgreSQL Lab6

- UPDATE COMPANY SET SALARY = SALARY * 1.5 WHERE ID IN (SELECT ID FROM COMPANY_BKP WHERE SALARY < 50000); This updates the salary in the COMPANY table by multiplying it by 1.5 for all records that have an ID present in the COMPANY_BKP table and whose salary is less than 50000. The subquery (SELECT ID FROM COMPANY_BKP WHERE SALARY < 50000) is used to filter the records in COMPANY table based on a condition on the COMPANY_BKP table. Only those records from COMPANY table are updated whose ID satisfies the condition in the subquery.

Uploaded by

Java Tech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

PostgreSQL lab6

Built In functions
• PostgreSQL built-in functions, also called as Aggregate functions, are used for performing
processing on string or numeric data.
• The following is the list of all general-purpose PostgreSQL built-in functions −
• PostgreSQL COUNT Function − The PostgreSQL COUNT aggregate function is used to count the
number of rows in a database table.
• PostgreSQL MAX Function − The PostgreSQL MAX aggregate function allows us to select the
highest (maximum) value for a certain column.
• PostgreSQL MIN Function − The PostgreSQL MIN aggregate function allows us to select the lowest
(minimum) value for a certain column.
• PostgreSQL AVG Function − The PostgreSQL AVG aggregate function selects the average value for
certain table column.
• PostgreSQL SUM Function − The PostgreSQL SUM aggregate function allows selecting the total for
a numeric column.
• PostgreSQL ARRAY Functions − The PostgreSQL ARRAY aggregate function puts input values,
including nulls, concatenated into an array.
• PostgreSQL Numeric Functions − Complete list of PostgreSQL functions required to manipulate
numbers in SQL.
• PostgreSQL String Functions − Complete list of PostgreSQL functions required to manipulate
strings in PostgreSQL.
Count()

• SELECT COUNT(*) FROM COMPANY ;

• SELECT COUNT(*) FROM COMPANY WHERE


name='Paul';
Max()
• SELECT MAX(salary) FROM COMPANY;

• Find all the records with maximum value for each


name using the GROUP BY clause as follows −
• SELECT id, name, MAX(salary) FROM COMPANY
GROUP BY id, name;

• SELECT MIN(salary), MAX(salary) max FROM


company;
Avg()
• calculate the average of all the SALARY
• SELECT AVG(SALARY) FROM COMPANY;
• You can take athe verage of various records set
using the GROUP BY clause.
• The following example will take the average of all
the records related to a dept and you will have
the average salary of dept person.
• SELECT dept, AVG(SALARY) FROM COMPANY
GROUP BY dept;
Sum()
ARRAY_AGG
• function is used to concatenate the input
values including null into an array.
• SELECT ARRAY_AGG(SALARY) FROM
COMPANY;
• array_agg ---------------------------------------------
{20000,15000,20000,65000,85000,45000,100
00}
numeric functions are used primarily for
numeric manipulation
S. No. Name & Description

1 ABS() Returns the absolute value of numeric


expression.

ACOS() Returns the arccosine of numeric


2 expression. Returns NULL if the value is not
in the range -1 to 1.

ASIN() Returns the arcsine of numeric


3 expression. Returns NULL if value is not in
the range -1 to 1

4 ATAN() Returns the arctangent of numeric


expression.
5 ATAN2() Returns the arctangent of the two
variables passed to it.
6 CEIL() Returns the smallest integer value that
is not less than passed numeric expression
CEILING() Returns the smallest integer value
7 that is not less than passed numeric
expression
COS() Returns the cosine of passed numeric
8 expression. The numeric expression should
be expressed in radians.
9 COT() Returns the cotangent of passed
numeric expression.
10 DEGREES() Returns numeric expression
converted from radians to degrees.
EXP() Returns the base of the natural logarithm (e) raised to the power of passed
11
numeric expression.
FLOOR() Returns the largest integer value that is not greater than passed numeric
12 expression.
13 GREATEST() Returns the largest value of the input expressions.

14 LEAST() Returns the minimum-valued input when given two or more.

15 LOG() Returns the natural logarithm of the passed numeric expression.

16 MOD() Returns the remainder of one expression


by diving by another expression.
17 PI() Returns the value of pi
POW() Returns the value of one expression raised
18 to the power of another expression
POWER() Returns the value of one expression
19 raised to the power of another expression
20 RADIANS() Returns the value of passed expression
converted from degrees to radians.
ROUND() Returns numeric expression rounded to
21 an integer. Can be used to round an expression to
a number of decimal points
22 SIN() Returns the sine of numeric expression given
in radians.
23 SQRT() Returns the non-negative square root of
numeric expression.
24 TAN() Returns the tangent of numeric expression
expressed in radians.
PostgreSQL string functions are used
primarily for string manipulation.
S. No. Name & Description
ASCII() Returns numeric value of left-most
1 character
BIT_LENGTH() Returns length of argument
2
in bits
CHAR_LENGTH() Returns number of
3
characters in argument
CHARACTER_LENGTH() A synonym for
4
CHAR_LENGTH()
CONCAT_WS() Returns concatenate with
5
separator
6 CONCAT() Returns concatenated string
7 LCASE() Synonym for LOWER()
LEFT() Returns the leftmost number of
8
characters as specified
9 LENGTH() Returns the length of a string in
bytes
10 LOWER() Returns the argument in lowercase
11 LPAD() Returns the string argument, left-
padded with the specified string
12 LTRIM() Removes leading spaces
MID() Returns a substring starting from the
13
specified position
14 POSITION() A synonym for LOCATE()
15 QUOTE() Escapes the argument for use in an
SQL statement
16 REGEXP Pattern matching using regular
expressions
17 REPEAT() Repeats a string the specified
number of times
18 REPLACE() Replaces occurrences of a
specified string
19 REVERSE() Reverse the characters in a string
RIGHT() Returns the specified rightmost
20
number of characters
RPAD() Appends string the specified
21 number of times

22 RTRIM() Removes trailing spaces

SUBSTRING(), SUBSTR() Returns the


24
substring as specified

25 TRIM() Removes leading and trailing


spaces
26 UCASE() Synonym for UPPER()

27 UPPER() Converts to uppercase


EXERCISE
• Display all employee last names in which the third letter of the name is a.
• Display the last name, job, and salary for all employees whose jobs are either sales
representative or stock clerk and whose salaries are not equal to $2,500, $3,500, or
$7,000
• Modify your query lab_03_02.sql to add a column that subtracts the old salary from the
new salary
• Write a query that displays the last name (with the first letter uppercase and all other
letters
• lowercase) and the length of the last name for all employees whose name starts with the
letters J, A, or M. Give each column an appropriate label. Sort the results by the
employees’ last names.
• Create a query to display the last name and salary for all employees. Format the salary to
be 15 characters long, left-padded with the $ symbol. Label the column SALARY.
• Display each employee’s last name, hire date, and salary review date, which is the first
Monday after six months of service. Label the column REVIEW. Format the dates to
appear in the format similar to “Monday, the Thirty-First of July, 2000.”
Exercise
Exercise

List the max average salary department wise.


PostgreSQL Subquery

• You will learn how to use the PostgreSQL


subquery that allows you to construct complex
queries.
• find the films whose rental rate is higher than
the average rental rate. We can do it in two
steps:
• The following query gets the average rental rate:
• SELECT AVG (rental_rate) FROM film;
we can get films whose rental rate is higher
than the average rental rate:
• SELECT film_id, title, rental_rate FROM film
WHERE rental_rate > 2.98;
• A subquery is a query nested inside another
query such as SELECT, INSERT, DELETE and
UPDATE
• construct a subquery, we put the second
query in brackets and use it in the
WHERE clause as an expression:
• SELECT film_id, title, rental_rate FROM film
WHERE rental_rate > ( SELECT AVG
(rental_rate) FROM film );
• The query inside the brackets is called a
subquery or an inner query. The query that
contains the subquery is known as an outer
query.
Subqueries with the INSERT Statement

• Subqueries also can be used with INSERT


statements.
• The INSERT statement uses the data returned
from the subquery to insert into another
table.
• The selected data in the subquery can be
modified with any of the character, date, or
number functions.
• Consider a table COMPANY_BKP, with similar
structure as COMPANY table and can be
created using the same CREATE TABLE using
COMPANY_BKP as the table name.

• INSERT INTO COMPANY_BKP SELECT * FROM


COMPANY WHERE ID IN (SELECT ID FROM
COMPANY) ;
Subqueries with the UPDATE Statement

• The subquery can be used in conjunction with the UPDATE


statement. Either single or multiple columns in a table can be
updated when using a subquery with the UPDATE statement.
• Assuming, we have COMPANY_BKP table available, which is
backup of the COMPANY table.
• The following example updates SALARY by 0.50 times in the
COMPANY table for all the customers, whose AGE is greater
than or equal to 27 −
• UPDATE COMPANY SET SALARY = SALARY * 0.50 WHERE AGE
IN (SELECT AGE FROM COMPANY_BKP WHERE AGE >= 27 );
Subqueries with the DELETE Statement

• Assuming, we have COMPANY_BKP table available,


which is a backup of the COMPANY table.
• The following example deletes records from the
COMPANY table for all the customers, whose AGE
is greater than or equal to 27 −
• DELETE FROM COMPANY WHERE AGE IN (SELECT
AGE FROM COMPANY_BKP WHERE AGE > 27 );
• This would affect two rows and finally the
COMPANY table

You might also like