You are on page 1of 30

Using Advanced SQL

Department of Information Systems School of Information and Technology


The SELECT command
To retrieve data from a table, the table is queried An SQL SELECT statement is used to do this. The statement is divided into:
A select list (the part that lists the columns to be returned), A table list (the partthat lists the tables from which to retrieve the data) An optional qualification (the part that specifies any restrictions) For example, to retrieve all the rows of table Customer: SELECT * FROM Cutomer;


The SELECT command format


.The DISTINCT Clause [ALL | DISTINCT [ON (expression [. 4 ."Customer". "State" from store...] ) ] ] ALL = all records returned in the result set (default) Default: DISTINCT only eliminates records that are complete duplicates ON option to define which column to compare for duplicates select distinct on ("City") "City".

"FirstName" as "Person" from store.. "LastName" as "Family"."Customer"... 5 .The SELECT List * | expression [AS output_name ] [. ] AS option allows you to change the column heading label in the output to a value different from the column name select "CustomerID" as "ID".

.] The most complex part of the SELECT command 6 ...The FROM Clause FROM from_list [.

Standard Table Names [ONLY ] table_name [ * ] [ [ AS ] alias [ (column_alias [..] ) ] ] ONLY option directs PostgreSQL to search only the table specified. and not any tables that inherit the specified table * directs PostgreSQL to search all child tables of the specified table 7 ...

"Name")..] ) ] select * from (select "CustomerID".SubThe Sub-select ( select ) [ AS ] alias [ (column_alias [. 8 .."Customer") as test ("ID". "FirstName" from store..

Functions The result set of the declared function is used as the input to the first SELECT command 9 .

. "Customer".]) ] NATURAL keyword is used to join tables on common column names USING keyword to define specific matching column names in both tables ON keyword to define a join condition select "Customer"."FirstName". "Order"."TotalCost" from store.."Customer“ natural inner join store."Product". 10 ."Order" natural inner join store.Joins from_item [ NATURAL ] join_type from_item [ ON join_condition | USING (join_column [."LastName"."ProductName".. "Product".

ProductID = Order. Order WHERE Product. Queries can access multiple tables at once Access the same table in such a way that multiple rows of the table are being processed at the same time => called a join query Select the pairs of rows where these values match: SELECT * FROM Product.ProductID .ProductID = Order. OR SELECT * FROM Product INNER JOIN Order ON (Product.ProductID .) 11 .Inner Joins Queries have only accessed one table at a time.

There are also right outer joins and full outer joins 12 .ProductID).Outer Joins SELECT * FROM Order LEFT OUTER JOIN Product ON (Order. empty (null) values are substituted for the right-table columns.ProductID= Product. This query is called a left outer join because the table mentioned on the left of the join operator will have each of its rows in the output at least once. When outputting a left-table row for which there is no righttable match. whereas the table on the right will only have those rows output that match some row of the left table.

weather W2 WHERE W1. W2.temp_lo < W2. 13 .Self Join We can also join a table against AS high.temp_lo AS low.temp_lo AND W1.temp_hi > W2.temp_lo AS low. W2.temp_hi.temp_hi AS high FROM weather W1. Example: suppose we wish to find all the weather records that are in the temperature range of other weather records. so we need to compare the temp_lo and temp_hi columns of each weather row to the temp_lo and temp_hi columns of all other weather rows: SELECT W1. W1. W2. This is called a self join.

SELECT max(UnitPrice) FROM Product. avg (average).Aggregate Functions An aggregate function computes a single result from multiple input rows. For example. //WRONG TRUE: => using a subquery: SELECT ProductName FROM Product WHERE UnitPrice = (SELECT max(UnitPrice ) FROM Product ). 14 . SELECT ProductName FROM Product WHERE UnitPrice = max(UnitPrice). there are aggregates to compute the count. max (maximum) and min (minimum) over a set of rows. sum.

Aggregate Functions Aggregates are also very useful in combination with GROUP BY clauses: SELECT OrderID. 15 . max(TotalCost) FROM Order GROUP BY ProductID.

OR. 16 .] Arbitrary Boolean operators (AND. and NOT) are allowed in the qualification of a query: SELECT * FROM Product WHERE ProductName= ’Soup’ AND UnitPrice> 30..The WHERE Clause WHERE condition [...

"Order" group by "ProductID".."ProductID" from store."Quantity").The GROUP BY Clause GROUP BY expression [...] GROUP BY clause be always used with a PostgreSQL function that aggregates values from similar records select sum("Order". "Order". Be careful with the GROUP BY and ORDER BY clauses GROUP BY clause groups similar records BEFORE the rest of the SELECT command is evaluated ORDER BY clause orders records AFTER the SELECT commands are processed 17 .

The HAVING Clause HAVING condition [.. 18 ..] The HAVING clause is similar to the WHERE clause.. in that it is used to define a filter condition to limit records used in the GROUP BY clause Records that do not satisfy the WHERE conditions are not processed by the GROUP BY clause The HAVING clause filters the records contained in the result set after the GROUP BY clause groups the records.

duplicate records in the output set are not displayed ALL 19 .The Set Operation Clauses select1 [ (UNION | INTERSECT | EXCEPT ]) [ ALL ] select2 The Set Operation clause types are UNION Display all result set records in both select1 and select2 INTERSECT Display only result set records that are in both select1 and select2 EXCEPT Display only result set records that are in select1 but not in select2 By default.

] ] By default.The ORDER BY Clause [ ORDER BY expression [ ASC | DESC | USING operator ] [... the ORDER BY clause orders records in ascending order The USING parameter declares an alternative operator to use for ordering (<) is equivalent to the ASC keyword (>) is equivalent to the DESC keyword 20 ..

The LIMIT Clause [ LIMIT ( count | ALL ) ] [ OFFSET start ] The LIMIT clause specifies a maximum number of records to return in the result set The default behavior is LIMIT ALL. which returns all records in the result set The OFFSET parameter allows you to specify the number of result set records to skip before displaying records in the output first record in the result set is at start value 0 Start value 1 is the second record 21 .

The FOR Clause [ FOR (UPDATE | SHARE ) [ OF table_name [.. the SELECT command does not wait.] [ NOWAIT ] ] FOR UPDATE locks the records (viewing. but instead exits with an error stating that the records are locked FOR SHARE clause allows other users to view the records If you do not want to lock all of the records returned in the result set.. or modifying) NOWAIT parameter.. deleting. combine the FOR clause with the LIMIT clause 22 .

Views Have covered the basics of using SQL to store and access our data in PostgreSQL. Here. but you don’t want to type the query each time you need it. TotalCost FROM Product. Quantity. which gives a name to the query that you can refer to like an ordinary table CREATE VIEW myview AS SELECT ProductName. OrderID. discuss some more advanced features of SQL that simplify management and prevent loss or corruption of data. 23 . Suppose the combined listing of specified records is of particular interest to your application. Create a view over the query. Order WHERE ProductID= 2000. SELECT * FROM myview. Model.

behind consistent interfaces. Views can be used in almost any place a real table can be used. 24 . Building views upon other views is not uncommon.Views Making liberal use of views is a key aspect of good SQL database design Views allow to encapsulate the details of the structure of our tables. which may change as our application evolves.

then none of the steps affect the database at all 25 .Transactions Transactions are a fundamental concept of all database systems. if some failure occurs that prevents the transaction from completing. all-or-nothing operation. The intermediate states between the steps are not visible to other concurrent transactions. The essential point of a transaction is that it bundles multiple steps into a single.

Transactions For example: consider a bank database that contains balances for various customer accounts.00 WHERE name = ’Bob’.00 from Alice’s account to Bob’s account. 26 .100. UPDATE branches SET balance = balance + 100.00 WHERE name = (SELECT branch_name FROM accounts WHERE name = ’Alice’) UPDATE accounts SET balance = balance + 100. Simplifying outrageously. UPDATE branches SET balance = balance .100. Suppose that we want to record a payment of $100. the SQL commands for this might look like: UPDATE accounts SET balance = balance . as well as total deposit balances for branches.00 WHERE name = (SELECT branch_name FROM accounts WHERE name = ’Bob’).00 WHERE name = ’Alice’.

27 . Grouping the updates into a transaction gives us this guarantee.Transactions There are several separate updates involved to accomplish this rather simple operation. or none of them happen. A transaction is said to be atomic: from the point of view of other transactions. Bank’s officers will want to be assured that either all these updates happen. We need a guarantee that if something goes wrong partway through the operation. it either happens completely or not at all. none of the steps executed so far will take effect.

each one should not be able to see the incomplete changes made by others. In PostgreSQL. So our banking transaction would actually look like: 28 . So transactions must be all-or-nothing not only in terms of their permanent effect on the database. a transaction is set up by surrounding the SQL commands of the transaction with BEGIN and COMMIT commands.Transactions Another important property of transactional databases is closely related to the notion of atomic updates: when multiple transactions are running concurrently. but also in terms of their visibility as they happen.

If.00 WHERE name = ’Alice’. 29 . we decide we don’t want to commit (perhaps we just noticed that Alice’s balance went negative).Transactions BEGIN.etc etc COMMIT. and all our updates so far will be canceled. we can issue the command ROLLBACK instead of COMMIT. partway through the transaction. UPDATE accounts SET balance = balance . -.100.

30 .