Rapid postgresql learning, part 3

17

Click here to load reader

Transcript of Rapid postgresql learning, part 3

Page 1: Rapid postgresql learning, part 3

Rapid POSTGRESQL learning, PART-3BY: ALI MASUDIANPOUR [email protected]

RAPID POSTGRESQL LEARNING. 1

Page 2: Rapid postgresql learning, part 3

Combining Queries, UNIONUNION used to append the result of a query to the result of other query◦ It eliminated the duplicate rows

◦ SELECT [Fields name] FROM [Table name] [Conditions] UNIONSELECT [Fields name] FROM [Table name] [Conditions]

◦ Example:

◦ SELECT * FROM Ordinary_customers WHERE 1 UNION SELECT * FROM pro_customers

◦ To avoid elimination duplicate rows we use ALL keyword◦ SELECT [Fields name] FROM [Table name] [Conditions]

UNION ALLSELECT [Fields name] FROM [Table name] [Conditions]

RAPID POSTGRESQL LEARNING. 2

Page 3: Rapid postgresql learning, part 3

Combining Queries, INTERSECTINTERSECT returns the rows that appear in both queries, to be more clear INTERSECT returns only common rows◦ It also eliminates duplication

◦ SELECT [Fields name] FROM [Table name] [Conditions] INTERSECTSELECT [Fields name] FROM [Table name] [Conditions]

◦ Example:

◦ SELECT * FROM Ordinary_customers WHERE 1 INTERSECT SELECT * FROM pro_customers

RAPID POSTGRESQL LEARNING. 3

Page 4: Rapid postgresql learning, part 3

Combining Queries, EXCEPTEXCEPT returns all the queries that are in the query one and not in the query two◦ SELECT [Fields name] FROM [Table name] [Conditions]

EXCEPTSELECT [Fields name] FROM [Table name] [Conditions]

◦ Example:◦ SELECT * FROM Ordinary_customers WHERE 1 EXCEPT SELECT * FROM pro_customers

RAPID POSTGRESQL LEARNING. 4

Page 5: Rapid postgresql learning, part 3

Combining queries- notesIn combining queries always bear in mind that:◦ Both queries should be compatible while we are using UNION, INTERSECT and EXCEPT

◦ They must have similar columns and both columns must have similar data types

RAPID POSTGRESQL LEARNING. 5

Page 6: Rapid postgresql learning, part 3

Aggregate Functions◦ SUM

◦ Is used to calculate the summation of a columns value

◦ SELECT SUM([column name]) FROM [Table name];

◦ AVG◦ Is used to calculate the average of a columns value

◦ SELECT AVG([column name]) FROM [Table name];

◦ MAX◦ Is used to get the maximum value of a columns value

◦ SELECT MAX([column name]) FROM [table name];

◦ COUNT◦ Is used to get the count of rows per a column

◦ SELECT COUNT([Column name]) FROM [table name]

◦ We can use DISTINCT to ignore repeated values

◦ SELECT COUNT(DISTINCT [column name]) FROM [table name]

RAPID POSTGRESQL LEARNING. 6

Page 7: Rapid postgresql learning, part 3

GROUP BYUsed to group together those rows in the table that have same values in all columns listed in the group by clause.◦ SELECT [column name] FROM [Table name] GROUP BY [column name]

◦ Example:

◦ SELECT field1, COUNT(field2) FROM testable GROUP BY field1;

◦ We can group by more than one field

RAPID POSTGRESQL LEARNING. 7

Page 8: Rapid postgresql learning, part 3

HAVINGTo have a condition into GROUP BY clause we use HAVING ◦ SELECT [column name] FROM [table name] GROUP BY [column name] HAVING [condition]

◦ Example

◦ SELECT field1, field2 FROM tableTest GROYUP BY field1 HAVING SUM(field1)>=100000;

◦ Note:◦ WHERE is used to eliminate rows, but in front, HAVING is used to eliminate groups

◦ Example

◦ SELECT order_date, SUM(order_total) AS total FROM ordersGROUP BY order_dateHAVING order_date>’2012-09-12’

RAPID POSTGRESQL LEARNING. 8

Page 9: Rapid postgresql learning, part 3

JOINJoin is a query that accesses more than one table or more than one row of the same table at once.◦ We need a condition to make joins

◦ Most of the times, condition will be the PRIMARY KEY and FORIGN KEY in tables

RAPID POSTGRESQL LEARNING. 9

Page 10: Rapid postgresql learning, part 3

INNER JOINSelects all rows from both tables as long as there is a match between the columns in both table. To be more clear, if there are rows in the first table that do not have match in second table, those will not be listed as result.◦ Syntax

◦ SELECT [Column name] FROM [First table name] INNER JOIN[second table name] ON [Condition]

◦ Condition could be: firstTable.column1=secondTable.column2

◦ Note: rows that have not column2 in first table and rows that have not column1 in second table will not be listed.

◦ The join table will include the row for each row in the table that match in second table based on our join condition.

RAPID POSTGRESQL LEARNING. 10

Page 11: Rapid postgresql learning, part 3

OUTER JONThe outer join differs from inner join in that rows are returned in result even when there are no matches through the JOIN criteria on the second table.◦ It can be useful when there is a need to merge data from two tables and to include all rows from both

tables without depending on a match.

◦ OUTER JOIN have 3 types◦ RIGHT JOIN ( RIGHT OUTER JOIN )

◦ LEFT JOIN ( LEFT OUTER JOIN )

◦ FULL JOIN ( FULL OUTER JON )

RAPID POSTGRESQL LEARNING. 11

Page 12: Rapid postgresql learning, part 3

OUTER JOINS◦ LEFT JOIN

◦ In LEFT OUTER JOIN all rows in first table (table that is left to JOIN statement) will be shown in result, but rows in second table that do not have matches in first table will not be shown in result.

◦ RIGHT JOIN◦ In RIGHT OUTER JOIN all rows in second table (table that is right to JOIN statement) will be shown in result, but rows In first table

that do not have matches in second table will not be shown in result.

◦ FULL JOIN◦ All rows in both tables (first and second table) will be shown in result even if rows in both tables have no match.

◦ SYNTAX◦ Syntax is the same in all joins but the only thing that changes would be LEFT/RIGJT/FULL

◦ Example

◦ SELECT [Column name] FROM [First table name] LEFT/ RIGHT/ FULL JOIN[second table name] ON [Condition]

◦ Condition could be: firstTable.column1=secondTable.column2

RAPID POSTGRESQL LEARNING. 12

Page 13: Rapid postgresql learning, part 3

CROSS JOINThe CROSS JOIN creates a Cartesian product between two sets of data.◦ This type of join does not maintain any relationship between the sets.

◦ Instead return the result which is the number of first rows multiplied by the number of rows in the second table.

◦ it returns every possible combination of rows between the joined sets.

◦ Syntax◦ SELECT [column name] FROM [first table name] CROSS JOIN [second table name];

RAPID POSTGRESQL LEARNING. 13

Page 14: Rapid postgresql learning, part 3

Sub Query Expressions◦ Sub query expression is basically a query that used in another query

◦ Sub queries are used with expressions

◦ Types of sub queries◦ EXISTS

◦ NOT EXISTS

◦ IN

◦ NOT IN

RAPID POSTGRESQL LEARNING. 14

Page 15: Rapid postgresql learning, part 3

Sub Query Expressions◦ Exists

◦ Evaluates if the sub query returns any row

◦ SELECT [column name] FROM [table name]WHERE EXISTS (SELECT [column name] FROM [table name] WHERE [condition])

◦ NOT EXISTS ◦ NOT EXISTS is the opposite of EXISTS

◦ SELECT [column name] FROM [table name]WHERE NOT EXISTS (SELECT [column name] FROM [table name] WHERE [condition])

◦ IN◦ There is 2 sides

◦ 1: if the left hand side is an expression the right side must be a sub query

◦ 2: if any equal row in sub query found, it is true, otherwise false

◦ Syntax

◦ SELECT [column name] FROM [table name] WHERE [column name] IN (SELECT [column name] FROM [table name] WHERE [condition])

◦ NOT IN◦ NOT IN is the opposite of IN

RAPID POSTGRESQL LEARNING. 15

Page 16: Rapid postgresql learning, part 3

VIEWSViews are similar to tables, but they are not physically a material.◦ VIEWS can create virtual tables

◦ How to create a view◦ CREATE OR REPLACE VIEW [view name] AS [query]

◦ Example:

◦ CREATE OR REPLACE VIEW students ASSELECT id,name,lastName FROM tbl_students;

◦ Temporary VIEW◦ When we need a temporary view we use temporary views

◦ CREATE OR REPLACE TEMP VIEW [view name] AS [query]

◦ Usage◦ Using views is completely the same with ordinary tables

◦ SELECT * FROM [viewname]

◦ REMOVE◦ To remove a view we use the following syntax

◦ DROP VIEW [view name]

◦ Notes◦ Views are read only

◦ Views can not be written or updated

RAPID POSTGRESQL LEARNING. 16

Page 17: Rapid postgresql learning, part 3

END OF PART 3End of Part 3◦ In part 4 we will discuss about:

◦ TRANSACTIONS

◦ SYSTEM COLUMNS

◦ CHANGING TABLES AND COLUMNS

◦ LOGICAL OPERATIORS

◦ COMPARISON OPERATORS

◦ MATHEMATICAL OPERATORS

◦ STRING FUNCTIONS AND OPERATIONS

RAPID POSTGRESQL LEARNING. 17