DataBase System The select clause g sQL allows duplicates in relations as well as in query results. H To force the elimination of duplicates, insert the keyword distinct after select H Example: find the names of all branches in the loan relations, and remove duplicates SELECT DISTINCT branch name fROM loan w The keyword all specifies that duplicates not be removed SELECT ALL branch name FROM loan Haichang Gao, Software School, Xidian University 29
DataBase System Haichang Gao , Software School , Xidian University 29 The SELECT Clause SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword DISTINCT after select. Example: Find the names of all branches in the loan relations, and remove duplicates SELECT DISTINCT branch_name FROM loan ; The keyword all specifies that duplicates not be removed. SELECT ALL branch_name FROM loan ;
DataBase System TThe selec clause G An asterisk( )in the select clause denotes"all attributes SELECT N FROM loan e The select clause can contain arithmetic expressions involving the operation, +, =,* and / and operating on constants or attributes of tuples. H The query seLECt Loan number branch name, amount *k 100 FROM loan would return a relation that is the same as the loan relation, except that the value of the attribute amount is multiplied by 100 Haichang Gao, Software School, Xidian University 30
DataBase System Haichang Gao , Software School , Xidian University 30 The SELECT Clause An asterisk(*) in the select clause denotes “all attributes” SELECT * FROM loan ; The SELECT clause can contain arithmetic expressions involving the operation, +, –, , and /, and operating on constants or attributes of tuples. The query: SELECT loan_number, branch_name, amount 100 FROM loan ; would return a relation that is the same as the loan relation, except that the value of the attribute amount is multiplied by 100
DataBase System The Where Clause ce The Where clause specifies conditions that the result must satis E Corresponds to the selection predicate of the relational algebra E Example: Find all loan number at the perryridge branch with loan amounts greater than $1200 SELECT Loan number fROM loan WherE branch name= Perryridge' ANd amount> 1200 w Comparison results can be combined using the logical connectives AND, OR, and not Haichang Gao, Software School, Xidian University 31
DataBase System Haichang Gao , Software School , Xidian University 31 The WHERE Clause The WHERE clause specifies conditions that the result must satisfy. Corresponds to the selection predicate of the relational algebra. Example: Find all loan number at the Perryridge branch with loan amounts greater than $1200. SELECT loan_number FROM loan WHERE branch_name=‘ Perryridge’ AND amount>1200; Comparison results can be combined using the logical connectives AND, OR, and NOT
DataBase System The Where Clause g sQL includes a BETWEEN comparison operator E Example: Find the loan number of those loans with loan amounts between $90,000 and $100,000(that is, >$90,000 and≤$100.000 SELECT Loan number FROM loan WherE amount bETWEen 90000 AND 100000 Haichang Gao, Software School, Xidian University 32
DataBase System Haichang Gao , Software School , Xidian University 32 The WHERE Clause SQL includes a BETWEEN comparison operator Example: Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is, $90,000 and $100,000) SELECT loan_number FROM loan WHERE amount BETWEEN 90000 AND 100000 ;
DataBase System The froM Clause G The from clause lists the relations involved in the query H Corresponds to the Cartesian product operation of the relational algebra H Example: find the Cartesian product borrower x loan SELECT* FROM borrower, loan; Example: Find the name, loan number and loan amount of all customers having a loan at the perryridge branch SELECT cuStomer name, borrower loan number amount FROM borrower loan wherE borrower loan number loan loan number AND: branch name ="Perryridge'i Haichang Gao, Software School, Xidian University 33
DataBase System Haichang Gao , Software School , Xidian University 33 The FROM Clause The FROM clause lists the relations involved in the query Corresponds to the Cartesian product operation of the relational algebra. Example: Find the Cartesian product borrower × loan SELECT FROM borrower, loan ; Example: Find the name, loan number and loan amount of all customers having a loan at the Perryridge branch SELECT customer_name, borrower.loan_number, amount FROM borrower, loan WHERE borrower.loan_number = loan.loan_number AND branch_name = ‘Perryridge’ ;