Using Advanced SQL: Department of Information Systems School of Information and Technology
Using Advanced SQL: Department of Information Systems School of Information and Technology
Using Advanced SQL: Department of Information Systems School of Information and Technology
SubThe Sub-select
( select ) [ AS ] alias [ (column_alias [,...] ) ] select * from (select "CustomerID", "FirstName" from store."Customer") as test ("ID", "Name");
Functions
The result set of the declared function is used as the input to the first SELECT command
Joins
from_item [ NATURAL ] join_type from_item [ ON join_condition | USING (join_column [,...]) ]
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"."LastName", "Customer"."FirstName", "Product"."ProductName", "Order"."TotalCost" from store."Order" natural inner join store."Customer natural inner join store."Product";
10
Inner Joins
Queries have only accessed one table at a time. 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, Order WHERE Product.ProductID = Order.ProductID ; OR SELECT * FROM Product INNER JOIN Order ON (Product.ProductID = Order.ProductID ;)
11
Outer Joins
SELECT * FROM Order LEFT OUTER JOIN Product ON (Order.ProductID= Product.ProductID); 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, whereas the table on the right will only have those rows output that match some row of the left table. When outputting a left-table row for which there is no righttable match, empty (null) values are substituted for the right-table columns. There are also right outer joins and full outer joins
12
Self Join
We can also join a table against itself. This is called a self join. Example: suppose we wish to nd 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.city, W1.temp_lo AS low, W1.temp_hi AS high, W2.city, W2.temp_lo AS low, W2.temp_hi AS high FROM weather W1, weather W2 WHERE W1.temp_lo < W2.temp_lo AND W1.temp_hi > W2.temp_hi;
13
Aggregate Functions
An aggregate function computes a single result from multiple input rows. For example, there are aggregates to compute the count, sum, avg (average), max (maximum) and min (minimum) over a set of rows. SELECT max(UnitPrice) FROM Product; SELECT ProductName FROM Product WHERE UnitPrice = max(UnitPrice); //WRONG TRUE: => using a subquery: SELECT ProductName FROM Product WHERE UnitPrice = (SELECT max(UnitPrice ) FROM Product );
14
Aggregate Functions
Aggregates are also very useful in combination with GROUP BY clauses: SELECT OrderID, max(TotalCost) FROM Order GROUP BY ProductID;
15
16
18
By default, duplicate records in the output set are not displayed ALL
19
20
21
22
Views
Have covered the basics of using SQL to store and access our data in PostgreSQL. Here, discuss some more advanced features of SQL that simplify management and prevent loss or corruption of data. Suppose the combined listing of specified records is of particular interest to your application, but you dont want to type the query each time you need it. Create a view over the query, which gives a name to the query that you can refer to like an ordinary table CREATE VIEW myview AS SELECT ProductName, Model, OrderID, Quantity, TotalCost FROM Product, Order WHERE ProductID= 2000; SELECT * FROM myview;
23
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, behind consistent interfaces. Views can be used in almost any place a real table can be used. Building views upon other views is not uncommon.
24
Transactions
Transactions are a fundamental concept of all database systems. The essential point of a transaction is that it bundles multiple steps into a single, all-or-nothing operation. The intermediate states between the steps are not visible to other concurrent transactions. if some failure occurs that prevents the transaction from completing, then none of the steps affect the database at all
25
Transactions
For example: consider a bank database that contains balances for various customer accounts, as well as total deposit balances for branches. Suppose that we want to record a payment of $100.00 from Alices account to Bobs account. Simplifying outrageously, the SQL commands for this might look like:
UPDATE accounts SET balance = balance - 100.00 WHERE name = Alice; UPDATE branches SET balance = balance - 100.00 WHERE name = (SELECT branch_name FROM accounts WHERE name = Alice) UPDATE accounts SET balance = balance + 100.00 WHERE name = Bob; UPDATE branches SET balance = balance + 100.00 WHERE name = (SELECT branch_name FROM accounts WHERE name = Bob);
26
Transactions
There are several separate updates involved to accomplish this rather simple operation. Banks ofcers will want to be assured that either all these updates happen, or none of them happen. We need a guarantee that if something goes wrong partway through the operation, none of the steps executed so far will take effect. Grouping the updates into a transaction gives us this guarantee. A transaction is said to be atomic: from the point of view of other transactions, it either happens completely or not at all.
27
Transactions
Another important property of transactional databases is closely related to the notion of atomic updates: when multiple transactions are running concurrently, each one should not be able to see the incomplete changes made by others. So transactions must be all-or-nothing not only in terms of their permanent effect on the database, but also in terms of their visibility as they happen. In PostgreSQL, a transaction is set up by surrounding the SQL commands of the transaction with BEGIN and COMMIT commands. So our banking transaction would actually look like:
28
Transactions
BEGIN; UPDATE accounts SET balance = balance - 100.00 WHERE name = Alice; -- etc etc COMMIT; If, partway through the transaction, we decide we dont want to commit (perhaps we just noticed that Alices balance went negative), we can issue the command ROLLBACK instead of COMMIT, and all our updates so far will be canceled.
29
30