SQL Examples from Chapters 6&7:

Size: px
Start display at page:

Download "SQL Examples from Chapters 6&7:"

Transcription

1 MIS 311 BUSINESS DABABASE MANAGEMENT Prepared by the Course Instructor: Dr. Mustafa Eid Note please, because there is an incompatibility between the MS WORD editor and SQL View editor, do not run the SQL query directly from this Word Doc. First Copy the query from this Word document to the Note Pad editor and then copy the query from Note Pad to the SQL View editor to run it. SQL Examples from Chapters 6&7: SQL is not case sensitive in MS ACCESS, but Capital letters are used only to increase clarity. Creating Tables Because VENDOR AND PRODUCT tables already exist in the SALE_CO database, then you create the VENDOR and PRODUCT with new names such as VENDOR_NEW and PRODUCT_NEW. Also, since V_CODE is used as a foreign key in PRODUCT_NEW, you need to create VENDOR_NEW table before cresting PRODUCT_NEW table. CREATE TABLE VENDOR_NEW( V_CODE INTEGER NOT NULL UNIQUE, V_NAME VARCHAR(35) NOT NULL, V_CONTACT VARCHAR(15) NOT NULL, V_AREACODE CHAR(3) NOT NULL, V_PHONE CHAR(8) NOT NULL, V_STATE CHAR(2) NOT NULL, V_ORDER CHAR(1) NOT NULL, PRIMARY KEY (V_CODE)); CREATE TABLE PRODUCT_NEW( P_CODE VARCHAR(10) NOT NULL UNIQUE, P_DESCRIPT VARCHAR(35) NOT NULL, P_INDATE DATE NOT NULL, P_OHNAND SMALLINT NOT NULL, P_MIN SMALLINT NOT NULL, P_PRICE NUMBER NOT NULL, P_DISCOUNT NUMBER NOT NULL, V_CODE INTEGER, PRIMARY KEY (P_CODE), FOREIGN KEY (V_CODE) REFERENCES VENDOR_NEW); 1

2 Creating Domains CREATE DOMAIN Command does not work in MS ACCESS. The purpose of using domains is to provide automatic data validation. However, data validation in MS ACCESS can be done from Forms. Adding data to a Table INSERT INTO VENDOR_NEW VALUES (21225, "Bryson, Inc.", "Smithson", "615", " ", "TN", "Y"); Listing the table contents If you wanted to list all columns and all rows of a table, then use the following select command: SELECT * ; Notice * is referred to as wild card in SQL. This means SQL allows the use of some wild cards such as * and? in MS ACCESS. If you want to select only the columns: P_CODE and P_DESCRIPT, and all rows, the select statement should be written as follows: Select P_CODE, P_DESCRIPT From product; Updating Data in a Table To make a correction on or to modify existing data in row(s) of a Table, use the Update command. Update command works with only one table at a time, see the example below: Modify the in_date value to new date 01/18/2002 for product 13-Q2/P2 Update product Set p_indate = #01/18/2002# Where p_code = 13-Q2/P2 ; Notice to achieve the right results, you need enter the exact letter case of the product code value 13-Q2/P2. That is, Character based primary key values are case sensitive. Producing Partial Listing of one Table contents Problem 1: Produce a listing of the columns: P_DESCRIPT, P_INDATE, P_PRICE, V_CODE for all products supplied by the Vendor whose code is 21344? SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE WHERE V_CODE = 21344; 2

3 partial listing problem 1 query P_DESCRIPT P_INDATE P_PRICE V_CODE 7.25-in. pwr. saw blade 18-Jan-02 $ in. pwr. saw blade 13-Nov-01 $ Rat-tail file, 1/8-in. fine 15-Dec-01 $ Problem 2: Produce a listing of the columns: P_DESCRIPT, P_INDATE, P_PRICE, V_CODE for all products supplied by all Vendors but not including the Vendor whose code is SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE WHERE V_CODE <> 21344; partial listing problem 2 query P_DESCRIPT P_INDATE P_PRICE V_CODE Power painter, 15 psi., 3-nozzle 03-Nov-01 $ Hrd. cloth, 1/4-in., 2x50 15-Jan-02 $ Hrd. cloth, 1/2-in., 3x50 15-Jan-02 $ B&D jigsaw, 12-in. blade 30-Dec-01 $ B&D jigsaw, 8-in. blade 24-Dec-01 $ B&D cordless drill, 1/2-in. 20-Jan-02 $ Claw hammer 20-Jan-02 $ Hicut chain saw, 16 in. 07-Feb-02 $ in. metal screw, Mar-02 $ in. wd. screw, Feb-02 $ Steel matting, 4'x8'x1/6",.5" mesh 17-Jan-02 $ Problem 3: Produce a listing of the columns: P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE for all products priced less than 10. SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE WHERE P_PRICE <= 10; Partial Listing Problem 3 Query P_DESCRIPT P_ONHAND P_MIN P_PRICE Claw hammer $9.95 Rat-tail file, 1/8-in. fine $4.99 PVC pipe, 3.5-in., 8-ft $ in. metal screw, $ in. wd. screw, $8.45 Problem 4: Produce a listing of the columns: P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE for all products whose P_CODE is less than 1558-QW1. SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE WHERE P_CODE < 1558-QW1 ; 3

4 Partial Listing Problem 4 Query P_DESCRIPT P_ONHAND P_MIN P_PRICE Power painter, 15 psi., 3-nozzle 8 5 $ in. pwr. saw blade $ in. pwr. saw blade $17.49 Hrd. cloth, 1/4-in., 2x $39.95 Problem 5: Produce a listing of the columns: P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE, P_INDATE for all products whose P_INDATE is greater than or equals to 01/20/2002. Notice MS ACCESS DBMS sets the date format when used against logical operators in where clause, as enclosing dates with the hash wild card, as shown in the following example: SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE, P_INDATE WHERE P_INDATE >= #01/20/2002#; Partial Listing Problem 5 Query P_DESCRIPT P_ONHAND P_MIN P_PRICE P_INDATE B&D cordless drill, 1/2-in $ Jan-02 Claw hammer $ Jan-02 Hicut chain saw, 16 in $ Feb-02 PVC pipe, 3.5-in., 8-ft $ Feb in. metal screw, $ Mar in. wd. screw, $ Feb-02 Using Computed Columns and Column Aliases Problem 6: Produce a listing of the columns: P_DESCRIPT, P_ONHAND, P_PRICE with the total value of each of the products currently held in inventory. To produce the total value for each product, it is needed to multiply the quantity on hand P_ONHAND by the product price P_PRICE. SELECT P_DESCRIPT, P_ONHAND, P_PRICE, P_ONHAND*P_PRICE ; Using Computed Columns Problem 1 Query P_DESCRIPT P_ONHAND P_PRICE Expr1003 Power painter, 15 psi., 3-nozzle 8 $ $ in. pwr. saw blade 32 $14.99 $ in. pwr. saw blade 18 $17.49 $ Hrd. cloth, 1/4-in., 2x50 15 $39.95 $ Hrd. cloth, 1/2-in., 3x50 23 $43.99 $1, B&D jigsaw, 12-in. blade 8 $ $ B&D jigsaw, 8-in. blade 6 $99.87 $ B&D cordless drill, 1/2-in. 12 $38.95 $ Claw hammer 23 $9.95 $ Sledge hammer, 12 lb. 8 $14.40 $ Rat-tail file, 1/8-in. fine 43 $4.99 $

5 Using Computed Columns Problem 1 Query P_DESCRIPT P_ONHAND P_PRICE Expr1003 Hicut chain saw, 16 in. 11 $ $2, PVC pipe, 3.5-in., 8-ft 188 $5.87 $1, in. metal screw, $6.99 $1, in. wd. screw, $8.45 $2, Steel matting, 4'x8'x1/6",.5" mesh 18 $ $2, Use the alias TOTVALUE for the result of the multiplication of P_ONHAND by P_PRICE SELECT P_DESCRIPT, P_ONHAND, P_PRICE, P_ONHAND*P_PRICE as TOTVALUE ; Using Computed Columns Problem 2 P_DESCRIPT P_ONHAND P_PRICE TOTVALUE Power painter, 15 psi., 3-nozzle 8 $ $ in. pwr. saw blade 32 $14.99 $ in. pwr. saw blade 18 $17.49 $ Hrd. cloth, 1/4-in., 2x50 15 $39.95 $ Hrd. cloth, 1/2-in., 3x50 23 $43.99 $1, B&D jigsaw, 12-in. blade 8 $ $ B&D jigsaw, 8-in. blade 6 $99.87 $ B&D cordless drill, 1/2-in. 12 $38.95 $ Claw hammer 23 $9.95 $ Sledge hammer, 12 lb. 8 $14.40 $ Rat-tail file, 1/8-in. fine 43 $4.99 $ Hicut chain saw, 16 in. 11 $ $2, PVC pipe, 3.5-in., 8-ft 188 $5.87 $1, in. metal screw, $6.99 $1, in. wd. screw, $8.45 $2, Steel matting, 4'x8'x1/6",.5" mesh 18 $ $2, Logical Operators: AND, OR, and NOT OR Problem 7: Produce a listing of the columns: P_DESCRIPT, P_INDATE, P_PRICE, V_CODE for all products supplied by the Vendors whose codes are and SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE WHERE V_CODE = OR V_CODE = 24288; 5

6 AND Problem 8: Produce a listing of the columns: P_DESCRIPT, P_INDATE, P_PRICE, V_CODE for all products whose price is less than and P_INDATE is greater than 01/05/2002. SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE WHERE P_PRICE < 50 AND P_INDATE > #01/05/2002#; Problem 9: Produce a listing of the columns: P_DESCRIPT, P_INDATE, P_PRICE, V_CODE for all products whose price is less than and P_INDATE is greater than 01/05/2002, also included in the listing all products supplied by the vendor whose vendor code is SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE WHERE (P_PRICE < 50 AND P_INDATE > #01/05/2002#) OR V_CODE = 24288; PRECEDENCE OF PROCESSING ARITHMETIC OPERATORS 1. If there exist Arithmetic Operators within parentheses ( ), they are done first, then the operators outside the parentheses will be done next 2. Power operator 3. Multiplication * 4. Division / 5. Addition + 6. Subtraction Notice arithmetic operators within parentheses will be processed in the sequence stated in 2 to 6 above. NOT operator is similar t o the operator not equals to <>. Also, MS ACCESS does not support NOT operator. Example: SELECT * WHERE V_CODE NOT 21344; 6

7 Special Operators BETWEEN Problem 10: List all products whose prices are between $50.00 and $ SELECT * WHERE P_PRICE BETWEEN AND ; If the DBMS SQL you use does not support BETWEEN, then the following Query will produce the same listing requested in problem 10. SELECT * WHERE P_PRICE > AND P_PRICE < ; IS NULL is used to check for a null data entry (or an attribute/column has null value) Problem 11: List all products whose product minimum value (P_MIN) is not stated or not entered (or NULL). SELECT P_CODE, P_DESCRIPT, P_MIN WHERE P_MIN IS NULL; Another example: List all products whose product P_INDATE value is not stated or not entered (or NULL). SELECT P_CODE, P_DESCRIPT, P_INDATE WHERE P_INDATE IS NULL; Another example: List all products whose supplying vendors are not known, or V_CODE value is not stated or not entered (or NULL). SELECT P_CODE, P_DESCRIPT, V_CODE WHERE V_CODE IS NULL; LIKE is used to check for similar characters string. Notice, in most SQL implementations, the matching with LIKE operator is case sensitive, that is, you need to enter the exact value you want to match to. LIKE must be used in 7

8 conjunction with wildcard characters. MS ACCESS uses * and _. Also, a combination of wildcards can be used with LIKE. Problem 12: List Contact information for all vendors whose contact name begins with Smith. SELECT V_NAME, V_CONTACT, V_AREACODE, V_PHONE FROM VENDOR WHERE V_CONTACT LIKE Smith* ; If you want to list all contacts that ends with son then put the wildcard * before the string of characters. For example: SELECT V_NAME, V_CONTACT, V_AREACODE, V_PHONE FROM VENDOR WHERE V_CONTACT LIKE *son ; If you are not sure the Vendor s Contact is t spelled as Smithson or Smithsen, you can use the LIKE operator with _ wild card as follows: SELECT * FROM VENDOR WHERE V_CONTACT LIKE Smith_n ; IN is used to check whether and attribute value matches a value contained within a subset of listed values. Problem 13: Produce a listing of all columns for all products supplied by the Vendors whose codes are and SELECT * WHERE V_CODE IN (21344, 24288); Notice, the same listing may be produced from the query: SELECT * WHERE V_CODE = OR V_CODE = 24288; EXISTS is used to check whether an attribute has a value. So, EXISTS is the opposite of IS NULL operator. EXISIS is not supported by MS ACESS Problem 14: List all products with existing (non-null) vendor codes. SELECT * WHERE V_CODE EXISTS; 8

9 Notice that the required listing may also be produced by applying NOT NULL as shown in the following query: SELECT * WHERE V_CODE IS NOT NULL; ADVANCED SQL COMMANDS DATA DEFINITION COMMANDS ALTER command is used to change table structures. ALTER is used: To change attributes/columns data types of characteristics, To add new columns to a table, or To drop or delete an existing column in a table. Remember since you are not the owner of the SALES COMPANY DATA BASE, then you can not run the ALTER command. You must be either the owner or you have been granted the access right to run ALTER command. In practice, usually the database administrator (DBA) who manages the database uses the ALTER command and can give access right to use the ALTER command. Try to create a new database for the sales company yourself using the MS ACCESS, and name SALES_01 for example. Then create the tables and add few rows, then try to use the ALTER command. CHANGING A COLUMNS DATA TYPE Problem 15: change the V_CODE from Integer to CHAR(5). ALTER TABLE VENDOR MODIFY (V_CODE CHAR(5)); To check the result of your query, select the table VENDOR and double click the view design to show the table VENDOR columns and their data types. ADDING A COLUMN TO THE TABLE Problem 16: add the new column P_SALECODE to the PRODUCT table. ALTER TABLE PRODUCT ADD (P_SALECODE CHAR(1)); 9

10 Check the result of your query. DROPPING A COLUMN Problem 17: delete the column P_ORDER from the VENDOR table. ALTER TABLE VENDOR DROP COLUM P_ORDER; Check the result of your query. ENTERING DATA INTO THE NEW COLUMN It is not possible to enter data into new column(s) in a table, because INSERT is used to enter data for the whole row values, which includes all columns in the row. Therefore, to enter data for one column value in a row, the UPDATE command must be used. Problem 18: enter the value 1 for the new column SALECODE in product whose CODE is 2232/QWE and 2232/QTY in the PRODUCT table. UPDATE PRODUCT SET P_SALECODE = 1 WHERE P_CODE IN ( 2232/QWE, 2232/QTY ); Check the result of your query. COPYING PARTS OF TABLES You can copy data from an old table to a new table with columns that matching data types. Problem 19: create a new table named PART and insert its data from the PRODUCT table by copying data from the PRODUCT table. CREATE TABLE PART ( PART_CODE PART_DESCRIPT PART_PRICE PRIMARY KEY CHAR(10) NOT NULL UNIQUE, VARCHAR(35), NUMBER, (PART_CODE)); Check the result of the CREATE query. INSERT INTO PART (PART_CODE, PART_DESCRIPT, PART_PRICE) SELECT P_CODE, P_DESCRIPT, P_PRICE ; Notice, in this case the data types of the new table: PART must match the PRODUCT table from which the data is copied. Check the results of the INSERT SELECT query. 10

11 DELETING A TABLE FROM THE DATABASE To delete a table from use the DROP command. Problem 20: delete the PART table. DROP TABLE PART; PRIMARY AND FOREIGN KEY DESIGNATON In case of importing tables between relational databases, the new imported table will not be assigned a primary key or foreign key. Then you need to assign a primary key and their foreign keys if they need foreign keys. Problem 21: Assuming that the PRODUCT and LINE tables are imported to the Sales Company Database. Add the primary key P_CODE to the PRODUCT table, and add the foreign key V_CODE to the PRODUCT table. ALTER TABLE PRODUCT ADD PRIMARY KEY (P_CODE); ALTER TABLE PRODUCT ADD FOREIGN KEY (V_CODE) REFERENCES VENDOR; Problem 22: Add the composite primary key INV_NUMBER and LINE_NUMBER to the LINE table. Also, add the foreign keys INV_NUMBER and PROD_CODE to the LINE table. ALTER TABLE LINE ADD PRIMARY KEY (INV_NUMBER, LINE_NUMBER) ADD FOREIGN KEY (INV_NUMBER) REFERENCES INVOICE ADD FOREIGN KEY (PROD_CODE) REFERENCES PRODUCT; MORE COMPLEX QUERIES AND SQL COMMANDS ORDERING A LISTING The ORDER BY is used to sort the output listing (rows) in an ascending or descending order. The sorting is based on one or more columns. By default, the ordering is done in an ascending order. Therefore, you do not need to specify ASCENDING, if you wanted the listing sorted in an ascending order. Otherwise, you specify DESC if you wanted the listing sorted in a descending order. 11

12 Problem 23: Produce a listing of all products by price in an Ascending order, and including product codes, description, in date, and price. SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE ORDER BY P_PRICE; Problem 24: Produce a listing of all products by price in a Descending order, and including product codes, description, in date, and price. SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE ORDER BY P_PRICE DESC; You can order the output listing based on more than one column, see the problem below. Problem 25: produce a telephone listing (phone directory) for all employees working in the organization, sorted by Last name, first name and middle name (employee initial). SELECT EMP_LNAME, EMP_FNAME, EMP_INITIAL, EMP_AREACODE, EMP_PHONE FROM EMPLOYEE ORDER BY EMP_LNAME, EMP_FNAME, EMP_INITIAL; Problem 26: produce a listing for all products whose price is 50 or less, and their product in date is less than 01/21/2002. The listing should be sorted by vendor code and product price in a descending order. SELECT P_CODE, P_DESCRIPT, P_INDATE, V_CODE, P_PRICE WHERE P_INDATE < #01/21/2002# AND P_PRICE <= 50 ORDER BY V_CODE, P_PRICE DESC; Listing Unique Values DISTINCT is used to generate only the unique values stored in one column. Problem 27: produce a listing of vendor codes from the PRODUCT table. The list must not have duplicate vendors. SELECT DISTINCT V_CODE ; 12

13 Aggregate Functions in SQL SQL have the following mathematical built in mathematical functions: COUNT MIN MAX SUM AVG COUNT is used to calculate (tally) the number of value stored in an attribute or column in a table. Problem 28: Calculate the number of actual vendors supplying products. SELECT COUNT(V_CODE) ; DISTICT is used in the above query because the vendor code V_CODE has duplicate values. Try rewriting and running the query without the DISTINCT, and check the result. The resulting count value will be bigger because all vendors stored in the V_CODE in the PRODUCT table will counted, which is wrong. Also, null values are not counted with the COUNT function. Problem 29: Calculate the number of actual vendors supplying products whose prices are less than or equals to $ SELECT COUNT(V_CODE) WHERE P_PRICE <= 10.00; MAX is used to find the maximum value from amongst the values of a column in a table. Problem 30: find the highest product price in the product table. SELECT MAX(P_PRICE) ; Problem 31: List product code, product description, and price of the product that has the highest price in the product table. SELECT P_CODE, P_DESCRIPT, P_PRICE WHERE P_PRICE = (SELECT MAX(P_PRICE) FROM PRODUCT); 13

14 This query is called nested query as it has inner query (SELECT MAX(P_PRICE ) and an outer query which is SELECT P_CODE, P_DESCRIP, P_PRICE WHERE P_PRICE =. The inner query is done (processed) first and after that the outer query, for the simple reason because the WHERE clause (that compares between the P_PRICE and maximum price) in the outer query depends on the maximum price value to be generated from the inner query. MIN is used to find the minimum value from amongst the values of a column in a table. Problem 32: find the lowest product price in the product table. SELECT MIN(P_PRICE) ; Problem 33: List product code, product description, and price of the product that has the lowest price in the product table. SELECT P_CODE, P_DESCRIPT, P_PRICE WHERE P_PRICE = (SELECT MIN(P_PRICE) FROM PRODUCT); SUM computes the total for any specified column or attribute, using any condition(s) imposed. Problem 34: find the total value of all the items (products) carried in the inventory (store). SELECT SUM(P_ONHAND*P_PRICE) ; Problem 35: find the total value of all the items (products) carried in the inventory (store), supplied by vendor whose code is SELECT SUM(P_ONHAND*P_PRICE) WHERE V_CODE = 21344; AVG is used to calculate the average values of a certain column. Problem 36: Find the average product price of all products in the product table. SELECT AVG(P_PRICE) ; 14

15 Problem 37: find a list of all products with the price that exceeds the average product price of all products stored in the product table. Order the output listing by price in a descending order. SELECT P_CODE, P_DESCRIPT, P_PRICE, V_CODE WHERE P_PRICE > (SELECT AVG(P_PRICE) ) ORDER BY P_PRICE DESC; GROUPING DATA GROUP BY Grouping data is useful for the generation of frequency distribution information. The grouping of data is achieved by using the GROUP BY clause. Since the grouping of data generates distinct groups with their corresponding calculated values such as count, max, min, average or sum, the GROUP BY is valid only with the SQL built in function (COUNT, MAX, MIN, SUM, and AVG). Problem 38: find the count of products supplied by each vendor in the product table. SELECT V_CODE, COUNT(P_CODE) GROUP BY V_CODE; Problem 39: find the count and average price of products supplied by each vendor in the product table. SELECT V_CODE, COUNT(P_CODE), AVG(P_PRICE) GROUP BY V_CODE; The GROUP BY features HAVING clause HAVING clause is applied to impose conditions on the listing generated from the GROUP BY command. Problem 40: find the count of products supplied by each vendor in the product table, and whose prices average below $ SELECT V_CODE, COUNT(DISTINCT(P_CODE)), AVG(P_PRICE) GROUP BY V_CODE HAVING AVG(P_PRICE) < 10; 15

16 In SQL, it is possible to combine multiple clauses and functions. See the following problem. Problem 41: produce a list contain the vendor code, and sum or total number of quantity on hand of products supplied by each vendors, but include in the list only those vendors having total number that exceed 5. The list must be sorted or ordered in a descending order by total number of quantity on hand. SELECT V_CODE, SUM(P_ONHAND) AS TotalQtyonhand GROUP BY V_CODE HAVING (SUM(P_ONHAND) > 5) ORDER BY SUM(P_ONHAND) DESC; VIEWS (VIRTUAL TABLES) Views are virtual (logical) tables which are only created for the program run time processing session. That is views are not stored permanently in the database like normal table, instead they are only stored in the main memory for the program run time processing. The purpose of creating views is to achieve complex calculation or processing of the data stored in the database tables. CREATING VIEWS Usually, the syntax of creating a view is combined with the select command in order to copy in the new created view data from an existing table. Then applied to the view the required calculation or processing without affecting the data stored in the original table where the data comes from. Problem 42: Create the view named PRODUCT_3 with only three columns: product description, product quantity on hand, and price from the product table. Include in the view only products with a price greater than $50. CREATE VIEW PRODUCT_3 AS SELECT P_DESCRIPT, P_ONHAND, P_PRICE WHERE P_PRICE > 50; Problem 43: list all products in the PRODUCT_3 view. SELECT * _3; Views are useful in making the data available to end users without having to worry about the corruption of data in the real tables, thereby maintaining higher level of data integrity. Views can improve data privacy too, because they enable you to restrict user o only view portions of the data from the real tables. 16

17 Views are dynamically updated. That is, if new products are added that meet the criterion P_PRICE > 50, those new products will automatically appear in the PRODUCT_3 view the next time the view is run or accessed. Also, views are often used as the basis for reports. Problem 44: Produce a report or listing that shows a summary of total product quantity on hand by vendor. CREATE VIEW SUMPRDXVEN AS SELECT V_CODE, SUM(P_ONHAND) AS TOTQTY, MAX(P_ONHAND) AS MAXQTY, MIN(P_ONHAND) AS MINQTY GROUP BY V_CODE; CREATING SQL INDEXES Indexes in SQL are used to improve efficiency of searching when using the select commands. CREATE UNIQUE INDEX P_CODEX ON PRODUCT(P_CODE); UNIQUE is needed here because P_CODE is a primary key. However, creating an index on an attribute that allows duplicate values, the create index query must not use UNIQUE feature. Also, to make efficient searching, use index on multiple attributes. Problem 45: Create an index on the vendor code (V_CODE) and P_CODE in PRODUCT table. CREATE INDEX VENPRODX ON PRODUCT(V_CODE, P_CODE); 17

18 JOINING DATABASE TABLES Joining tables is used to access data from multiple tables. Problem 46: Create a report listing that includes the following information: product description, product price, and the information (vendor name, contact, area-code, vendor phone) of supplying vendors of all products stored in table PRODUCT. SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT, V_AREACODE, V_PHONE, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE; Problem 47: Order the listing produced in problem 46, by price in an ascending order. SELECT P.P_DESCRIPT, P.P_PRICE, V.V_NAME, V.V_CONTACT, V.V_AREACODE, V.V_PHONE P, VENDOR V WHERE P.V_CODE = V.V_CODE ORDER BY PRICE; Notice, the query has used aliases to differentiate between same column names in different tables. Problem 48: Restrict the listing of problem 46, to produce only the products whose in date is greater than the date 01/15/2002. Order the listing by in date in an ascending order. SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT, V_AREACODE, V_PHONE, P_INDATE P, VENDOR V WHERE P.V_CODE = V.V_CODE AND P_INDATE > #01/15/2002# ORDER BY P_INDATE; Recursive Queries Recursive query is a SQL query that joins a table with itself. Problem 49: produce a listing of all employees and their managers names. The EMP table structure is shown below: EMP_NUM (Primary key) EMP_TITLE EMP_LNAME 18

19 EMP_FNAME EMP_INITIAL EMP_DOB EMP_HIRE_DATE EMP_AREA_CODE EMP_PHONE EMP_MGR (EMP_MGR acts as a foreign key which is based on the primary key EMP_NUM of the same table EMP) SELECT A.EMP_NUM, A.EMP_LNAME, A.EMP_MGR, B.EMP_LNAME FROM EMP A, EMP B WHERE A.EMP_MGR = B.EMP_NUM ORDER BY A.EMP_MGR; Notice, the table EMP is used twice, that is the DBMS creates another copy of the EMP table to accomplish the join operator between table EMP with itself. A.EMP_NUM, A.EMP_LNAME these two columns represents the employee number and employee last name in EMP copy A, and B.EMP_LNAME this column represents the employee last name of the manager in EMP copy B. EMP table (COPY A) EMP table (COPY B) EMP_NUM ENM_MGR (FK) EMP_NUM (PK) 110 ENM_MGR OUTER JOINS Left outer join in MS ACCESS is done as follows: SELECT P_CODE, VENDOR.[V_CODE], V_CODE FROM VENDOR LEFT JOIN PRODUCT ON VENDOR.[V_CODE] = PRODUCT.V_CODE; Right outer join in MS ACCESS is done as follows: SELECT P_CODE, VENDOR.[V_CODE], V_CODE FROM VENDOR RIGHT JOIN PRODUCT ON VENDOR.[V_CODE] = PRODUCT.V_CODE; 19

20 UPDATABLE VIEWS Common operation in production environments is use of batch routines to update master table attributes using transaction data Overnight batch jobs Not all views are updatable Restrictions GROUP BY expressions cannot be used Cannot use set operators---union, INTERSECTION, etc. In MS ACCESS it is possible to run UPDATE for updating one column in one table but requiring the use of a column in another table. That is using the UPDATE command with multiple tables (see example in page ). UPDATE PRODUCT, PMSALES SET PRODUCT.PROD_QOH = PROD_QOH PMS_QTY WHERE PRODUCT.PROD_ID = PMSALES.PROD_ID; This SQL query will first join the tables: PRODUCT and PMSALES, then it will update all products quantity on hand that have a matching value of PROD_ID in PMSALES. In Oracle, it is not possible to run the update command with multiple tables. Therefore, to achieve updating one column in a table based on a column exists in another table, two steps are needed: 1. a view is created first to include the columns needed from multiple tables, then 2. update the column need to be updated in the new view which will update the column in the original table PRODUCT See example in page 265. the steps shown below present the two queries: Step 1: CREATE VIEW PMSVUPD AS ( SELECT PRODUCT.PROD_ID, PRODUCT.PROD_QOH, PMSALES.PMS_QTY, PMSALES WHERE PRODUCT.PROD_ID = PMSALES.PROD_ID); Step 2: UPDATE PMSVUPD SET PROD_QOH = PROD_QOH PMS_QTY; 20

21 PROCEDURAL SQL SQL shortcomings SQL queries retrieves a set or rows (listing) Does not support conditional programming statements such as: IF-THEN-ELSE or Looping operations Solutions Embedded SQL can be called from within procedural programming languages such as COBOL, C, or Visual Basic Shared Code stored and executed within the database. To meet this requirement, most RDBMS vendors create a programming language extension (called Procedural SQL) to support end users procedural activities. Procedural SQL (PL/SQL) stored within the database, executed by DBMS, and invoked by the end user. Stored procedures include: Triggers Stored procedures PL/SQL functions TRIGGERS A trigger is procedural SQL code that is automatically invoked (run) by the DBMS upon the occurrence of a data manipulation event. A trigger is always run before or after a data row is selected, inserted, or one or column values are updated. A trigger is always associated with a database table. But a database table may have one or more triggers. A trigger is executed as part of the transaction that triggered it. Problem 50: Create a trigger that will automatically run if the column P_ONHAND is changed by an insert or update command. The trigger will compare the P_ONHAND with the P_MIN column. If the value of P_ONHAND is equal to or less than P_MIN, the trigger must update the P_REORDER to 1. The trigger s code is written in Oracle SQL*Plus. CREATE OR REPLACE TRIGGER TRG_PRODUCT_REORDER AFTER INSERT OR UPDATE OF P_ONHAND ON PRODUCT BEGIN UPDATE PRODUCT SET P_REORDER =1 WHERE P_ONHAND <= P_MIN; To check if the created trigger works, do run the following SQL queries: SELECT * 21

22 WHERE P_CODE = 11QER/31 ; UPDATE PRODUCT SET P_ONHAND = 4 WHERE P_CODE = 11QER/31 ; SELECT * WHERE P_CODE = 11QER/31 ; STORED PROCEDURES A Stored Procedure is a set of procedural and SQL statements. It is stored in the database it is related to, and invoked/run by name from the DBMS, that it is not run automatically like a Trigger. The following SQL*Plus syntax is used to create a stored procedure. CREATE OR REPLACE procedure name (argument IN/OUT data type, etc) IS/AS BEGIN DECLARE variable name and data type PL/SQL or SQL statements END; To invoke/run a stored procedure use the following syntax in SQL*Plus: EXEC stored-procedure _name (parameter, parameter, ) Problem 51: Create a stored procedure to set (or update) the P_REORDER column value to 1 in PORDUCT table when a product s P_ONHAMD value reaches or becomes less than the product s minimum quantity P_MIN value. Also, the procedure need to set the product s P_REORDER value to 0 when a product s P_ONHAMD value becomes greater than the product s minimum quantity P_MIN value. The following code is written in Oracle SQL*Plus SQL> RUN CREATE OR REPLACE PROCEDURE PROD_REORDER_SET AS BEGIN UPDATE PRODUCT SET P_REORDER = 1 WHERE P_ONHAND <= P_MIN; UPDATE PRODUCT SET P_REORDER = 0 WHERE P_ONHAND > P_MIN; END; 22

23 Procedure created SQL> EXEC PROD_REORDER_SET PL/SQL Procedure successfully completed SQL> PL/SQL STORED FUNCTIONS A stored function is similar to stored procedure, but stored function can only be run from a stored procedure or a trigger by calling it. It is basically a set of procedural and SQL statements that returns a value (indicated by the RETURN statement in its code). The syntax for creating a SQL stored function is CREATE FUNCTION function-name (argument IN data type, etc.) RETURN data type AS BEGIN PL/SQL statements RETURN (value); END; 23

ORACLE 10g Lab Guide

ORACLE 10g Lab Guide A supplement to: Database Systems: Design, Implementation and Management (International Edition) Rob, Coronel & Crockett (ISBN: 9781844807321) Table of Contents Lab Title Page 1 Introduction to ORACLE

More information

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals Oracle University Contact Us: 1.800.529.0165 Oracle Database: SQL and PL/SQL Fundamentals Duration: 5 Days What you will learn This course is designed to deliver the fundamentals of SQL and PL/SQL along

More information

3.GETTING STARTED WITH ORACLE8i

3.GETTING STARTED WITH ORACLE8i Oracle For Beginners Page : 1 3.GETTING STARTED WITH ORACLE8i Creating a table Datatypes Displaying table definition using DESCRIBE Inserting rows into a table Selecting rows from a table Editing SQL buffer

More information

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals Oracle University Contact Us: +966 12 739 894 Oracle Database: SQL and PL/SQL Fundamentals Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals training is designed to

More information

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals NEW Oracle University Contact Us: + 38516306373 Oracle Database: SQL and PL/SQL Fundamentals NEW Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals training delivers the

More information

Oracle SQL. Course Summary. Duration. Objectives

Oracle SQL. Course Summary. Duration. Objectives Oracle SQL Course Summary Identify the major structural components of the Oracle Database 11g Create reports of aggregated data Write SELECT statements that include queries Retrieve row and column data

More information

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Outline More Complex SQL Retrieval Queries

More information

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

SQL - QUICK GUIDE. Allows users to access data in relational database management systems. http://www.tutorialspoint.com/sql/sql-quick-guide.htm SQL - QUICK GUIDE Copyright tutorialspoint.com What is SQL? SQL is Structured Query Language, which is a computer language for storing, manipulating

More information

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff D80198GC10 Oracle Database 12c SQL and Fundamentals Summary Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff Level Professional Delivery Method Instructor-led

More information

Database Administration with MySQL

Database Administration with MySQL Database Administration with MySQL Suitable For: Database administrators and system administrators who need to manage MySQL based services. Prerequisites: Practical knowledge of SQL Some knowledge of relational

More information

Introduction to Microsoft Jet SQL

Introduction to Microsoft Jet SQL Introduction to Microsoft Jet SQL Microsoft Jet SQL is a relational database language based on the SQL 1989 standard of the American Standards Institute (ANSI). Microsoft Jet SQL contains two kinds of

More information

Database Query 1: SQL Basics

Database Query 1: SQL Basics Database Query 1: SQL Basics CIS 3730 Designing and Managing Data J.G. Zheng Fall 2010 1 Overview Using Structured Query Language (SQL) to get the data you want from relational databases Learning basic

More information

Microsoft Access 3: Understanding and Creating Queries

Microsoft Access 3: Understanding and Creating Queries Microsoft Access 3: Understanding and Creating Queries In Access Level 2, we learned how to perform basic data retrievals by using Search & Replace functions and Sort & Filter functions. For more complex

More information

SQL Simple Queries. Chapter 3.1 V3.0. Copyright @ Napier University Dr Gordon Russell

SQL Simple Queries. Chapter 3.1 V3.0. Copyright @ Napier University Dr Gordon Russell SQL Simple Queries Chapter 3.1 V3.0 Copyright @ Napier University Dr Gordon Russell Introduction SQL is the Structured Query Language It is used to interact with the DBMS SQL can Create Schemas in the

More information

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries COMP 5138 Relational Database Management Systems Week 5 : Basic COMP5138 "Relational Database Managment Systems" J. Davis 2006 5-1 Today s Agenda Overview Basic Queries Joins Queries Aggregate Functions

More information

Relational Database: Additional Operations on Relations; SQL

Relational Database: Additional Operations on Relations; SQL Relational Database: Additional Operations on Relations; SQL Greg Plaxton Theory in Programming Practice, Fall 2005 Department of Computer Science University of Texas at Austin Overview The course packet

More information

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query Objectives The objective of this lab is to learn the query language of SQL. Outcomes After completing this Lab,

More information

Database Applications Microsoft Access

Database Applications Microsoft Access Database Applications Microsoft Access Lesson 4 Working with Queries Difference Between Queries and Filters Filters are temporary Filters are placed on data in a single table Queries are saved as individual

More information

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA www.sybex.com

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA www.sybex.com Essential SQL 2 Essential SQL This bonus chapter is provided with Mastering Delphi 6. It is a basic introduction to SQL to accompany Chapter 14, Client/Server Programming. RDBMS packages are generally

More information

Financial Data Access with SQL, Excel & VBA

Financial Data Access with SQL, Excel & VBA Computational Finance and Risk Management Financial Data Access with SQL, Excel & VBA Guy Yollin Instructor, Applied Mathematics University of Washington Guy Yollin (Copyright 2012) Data Access with SQL,

More information

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to: D61830GC30 for Developers Summary Duration Vendor Audience 5 Days Oracle Database Administrators, Developers, Web Administrators Level Technology Professional Oracle 5.6 Delivery Method Instructor-led

More information

Oracle 10g PL/SQL Training

Oracle 10g PL/SQL Training Oracle 10g PL/SQL Training Course Number: ORCL PS01 Length: 3 Day(s) Certification Exam This course will help you prepare for the following exams: 1Z0 042 1Z0 043 Course Overview PL/SQL is Oracle's Procedural

More information

Advanced Query for Query Developers

Advanced Query for Query Developers for Developers This is a training guide to step you through the advanced functions of in NUFinancials. is an ad-hoc reporting tool that allows you to retrieve data that is stored in the NUFinancials application.

More information

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals NEW Oracle University Contact Us: 001-855-844-3881 & 001-800-514-06-97 Oracle Database: SQL and PL/SQL Fundamentals NEW Duration: 5 Days What you will learn This Oracle Database: SQL and PL/SQL Fundamentals

More information

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX ISSN: 2393-8528 Contents lists available at www.ijicse.in International Journal of Innovative Computer Science & Engineering Volume 3 Issue 2; March-April-2016; Page No. 09-13 A Comparison of Database

More information

Database Programming with PL/SQL: Learning Objectives

Database Programming with PL/SQL: Learning Objectives Database Programming with PL/SQL: Learning Objectives This course covers PL/SQL, a procedural language extension to SQL. Through an innovative project-based approach, students learn procedural logic constructs

More information

Oracle Database 10g: Introduction to SQL

Oracle Database 10g: Introduction to SQL Oracle University Contact Us: 1.800.529.0165 Oracle Database 10g: Introduction to SQL Duration: 5 Days What you will learn This course offers students an introduction to Oracle Database 10g database technology.

More information

SQL Programming. Student Workbook

SQL Programming. Student Workbook SQL Programming Student Workbook 2 SQL Programming SQL Programming Published by itcourseware, Inc., 7245 South Havana Street, Suite 100, Englewood, CO 80112 Contributing Authors: Denise Geller, Rob Roselius,

More information

ICAB4136B Use structured query language to create database structures and manipulate data

ICAB4136B Use structured query language to create database structures and manipulate data ICAB4136B Use structured query language to create database structures and manipulate data Release: 1 ICAB4136B Use structured query language to create database structures and manipulate data Modification

More information

9.1 SAS. SQL Query Window. User s Guide

9.1 SAS. SQL Query Window. User s Guide SAS 9.1 SQL Query Window User s Guide The correct bibliographic citation for this manual is as follows: SAS Institute Inc. 2004. SAS 9.1 SQL Query Window User s Guide. Cary, NC: SAS Institute Inc. SAS

More information

Instant SQL Programming

Instant SQL Programming Instant SQL Programming Joe Celko Wrox Press Ltd. INSTANT Table of Contents Introduction 1 What Can SQL Do for Me? 2 Who Should Use This Book? 2 How To Use This Book 3 What You Should Know 3 Conventions

More information

CIS 631 Database Management Systems Sample Final Exam

CIS 631 Database Management Systems Sample Final Exam CIS 631 Database Management Systems Sample Final Exam 1. (25 points) Match the items from the left column with those in the right and place the letters in the empty slots. k 1. Single-level index files

More information

A basic create statement for a simple student table would look like the following.

A basic create statement for a simple student table would look like the following. Creating Tables A basic create statement for a simple student table would look like the following. create table Student (SID varchar(10), FirstName varchar(30), LastName varchar(30), EmailAddress varchar(30));

More information

Access Queries (Office 2003)

Access Queries (Office 2003) Access Queries (Office 2003) Technical Support Services Office of Information Technology, West Virginia University OIT Help Desk 293-4444 x 1 oit.wvu.edu/support/training/classmat/db/ Instructor: Kathy

More information

Information Systems SQL. Nikolaj Popov

Information Systems SQL. Nikolaj Popov Information Systems SQL Nikolaj Popov Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria popov@risc.uni-linz.ac.at Outline SQL Table Creation Populating and Modifying

More information

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Objectives After completing this lesson, you should be able to do the following: List the capabilities of SQL SELECT statements Execute a basic SELECT statement

More information

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables RDBMS Using Oracle Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture Joining Tables Multiple Table Queries Simple Joins Complex Joins Cartesian Joins Outer Joins Multi table Joins Other Multiple

More information

Relational Databases

Relational Databases Relational Databases Jan Chomicki University at Buffalo Jan Chomicki () Relational databases 1 / 18 Relational data model Domain domain: predefined set of atomic values: integers, strings,... every attribute

More information

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database 12c: Introduction to SQL Ed 1.1 Oracle University Contact Us: 1.800.529.0165 Oracle Database 12c: Introduction to SQL Ed 1.1 Duration: 5 Days What you will learn This Oracle Database: Introduction to SQL training helps you write subqueries,

More information

How To Create A Table In Sql 2.5.2.2 (Ahem)

How To Create A Table In Sql 2.5.2.2 (Ahem) Database Systems Unit 5 Database Implementation: SQL Data Definition Language Learning Goals In this unit you will learn how to transfer a logical data model into a physical database, how to extend or

More information

SQL Basics. Introduction to Standard Query Language

SQL Basics. Introduction to Standard Query Language SQL Basics Introduction to Standard Query Language SQL What Is It? Structured Query Language Common Language For Variety of Databases ANSI Standard BUT. Two Types of SQL DML Data Manipulation Language

More information

Programming with SQL

Programming with SQL Unit 43: Programming with SQL Learning Outcomes A candidate following a programme of learning leading to this unit will be able to: Create queries to retrieve information from relational databases using

More information

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL) Copyright 2000-2001, University of Washington Using Multiple Operations Implementing Table Operations Using Structured Query Language (SQL) The implementation of table operations in relational database

More information

SQL SELECT Query: Intermediate

SQL SELECT Query: Intermediate SQL SELECT Query: Intermediate IT 4153 Advanced Database J.G. Zheng Spring 2012 Overview SQL Select Expression Alias revisit Aggregate functions - complete Table join - complete Sub-query in where Limiting

More information

Oracle For Beginners Page : 1

Oracle For Beginners Page : 1 Oracle For Beginners Page : 1 Chapter 22 OBJECT TYPES Introduction to object types Creating object type and object Using object type Creating methods Accessing objects using SQL Object Type Dependencies

More information

IT2305 Database Systems I (Compulsory)

IT2305 Database Systems I (Compulsory) Database Systems I (Compulsory) INTRODUCTION This is one of the 4 modules designed for Semester 2 of Bachelor of Information Technology Degree program. CREDITS: 04 LEARNING OUTCOMES On completion of this

More information

Netezza SQL Class Outline

Netezza SQL Class Outline Netezza SQL Class Outline CoffingDW education has been customized for every customer for the past 20 years. Our classes can be taught either on site or remotely via the internet. Education Contact: John

More information

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

Guide to Performance and Tuning: Query Performance and Sampled Selectivity Guide to Performance and Tuning: Query Performance and Sampled Selectivity A feature of Oracle Rdb By Claude Proteau Oracle Rdb Relational Technology Group Oracle Corporation 1 Oracle Rdb Journal Sampled

More information

Using AND in a Query: Step 1: Open Query Design

Using AND in a Query: Step 1: Open Query Design Using AND in a Query: Step 1: Open Query Design From the Database window, choose Query on the Objects bar. The list of saved queries is displayed, as shown in this figure. Click the Design button. The

More information

SQL. Short introduction

SQL. Short introduction SQL Short introduction 1 Overview SQL, which stands for Structured Query Language, is used to communicate with a database. Through SQL one can create, manipulate, query and delete tables and contents.

More information

IT2304: Database Systems 1 (DBS 1)

IT2304: Database Systems 1 (DBS 1) : Database Systems 1 (DBS 1) (Compulsory) 1. OUTLINE OF SYLLABUS Topic Minimum number of hours Introduction to DBMS 07 Relational Data Model 03 Data manipulation using Relational Algebra 06 Data manipulation

More information

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS...

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS... Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS... 3 CHANGE A JOIN PROPERTY... 4 REMOVING A JOIN... 4 CREATE QUERIES... 4 THE

More information

T-SQL STANDARD ELEMENTS

T-SQL STANDARD ELEMENTS T-SQL STANDARD ELEMENTS SLIDE Overview Types of commands and statement elements Basic SELECT statements Categories of T-SQL statements Data Manipulation Language (DML*) Statements for querying and modifying

More information

Answers to Selected Questions and Problems

Answers to Selected Questions and Problems Answers to Selected Questions and Problems-Edition8 Page 1 of 64 Answers to Selected Questions and Problems Chapter 1 Database Systems Answers to Selected Review Questions 2. Data redundancy exists when

More information

A Brief Introduction to MySQL

A Brief Introduction to MySQL A Brief Introduction to MySQL by Derek Schuurman Introduction to Databases A database is a structured collection of logically related data. One common type of database is the relational database, a term

More information

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL Oracle University Contact Us: +381 11 2016811 Oracle Database: Introduction to SQL Duration: 5 Days What you will learn Understanding the basic concepts of relational databases ensure refined code by developers.

More information

Oracle Database 11g SQL

Oracle Database 11g SQL AO3 - Version: 2 19 June 2016 Oracle Database 11g SQL Oracle Database 11g SQL AO3 - Version: 2 3 days Course Description: This course provides the essential SQL skills that allow developers to write queries

More information

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL Oracle University Contact Us: 1.800.529.0165 Oracle Database: Introduction to SQL Duration: 5 Days What you will learn View a newer version of this course This Oracle Database: Introduction to SQL training

More information

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan More on SQL Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan SELECT A1, A2,, Am FROM R1, R2,, Rn WHERE C1, C2,, Ck Interpreting a Query

More information

SQL Server. 1. What is RDBMS?

SQL Server. 1. What is RDBMS? SQL Server 1. What is RDBMS? Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained

More information

SQL Server An Overview

SQL Server An Overview SQL Server An Overview SQL Server Microsoft SQL Server is designed to work effectively in a number of environments: As a two-tier or multi-tier client/server database system As a desktop database system

More information

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL Oracle University Contact Us: 1.800.529.0165 Oracle Database: Introduction to SQL Duration: 5 Days What you will learn This Oracle Database: Introduction to SQL training teaches you how to write subqueries,

More information

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL Chapter 9 Joining Data from Multiple Tables Oracle 10g: SQL Objectives Identify a Cartesian join Create an equality join using the WHERE clause Create an equality join using the JOIN keyword Create a non-equality

More information

Ad Hoc Advanced Table of Contents

Ad Hoc Advanced Table of Contents Ad Hoc Advanced Table of Contents Functions... 1 Adding a Function to the Adhoc Query:... 1 Constant... 2 Coalesce... 4 Concatenate... 6 Add/Subtract... 7 Logical Expressions... 8 Creating a Logical Expression:...

More information

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems CSC 74 Database Management Systems Topic #0: SQL Part A: Data Definition Language (DDL) Spring 00 CSC 74: DBMS by Dr. Peng Ning Spring 00 CSC 74: DBMS by Dr. Peng Ning Schema and Catalog Schema A collection

More information

Introduction to SQL and database objects

Introduction to SQL and database objects Introduction to SQL and database objects IBM Information Management Cloud Computing Center of Competence IBM Canada Labs 1 2011 IBM Corporation Agenda Overview Database objects SQL introduction The SELECT

More information

Structured Query Language (SQL)

Structured Query Language (SQL) Objectives of SQL Structured Query Language (SQL) o Ideally, database language should allow user to: create the database and relation structures; perform insertion, modification, deletion of data from

More information

5. CHANGING STRUCTURE AND DATA

5. CHANGING STRUCTURE AND DATA Oracle For Beginners Page : 1 5. CHANGING STRUCTURE AND DATA Altering the structure of a table Dropping a table Manipulating data Transaction Locking Read Consistency Summary Exercises Altering the structure

More information

Programming Database lectures for mathema

Programming Database lectures for mathema Programming Database lectures for mathematics students April 25, 2015 Functions Functions are defined in Postgres with CREATE FUNCTION name(parameter type,...) RETURNS result-type AS $$ function-body $$

More information

SQL Server. 2012 for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

SQL Server. 2012 for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach TRAINING & REFERENCE murach's SQL Server 2012 for developers Bryan Syverson Joel Murach Mike Murach & Associates, Inc. 4340 N. Knoll Ave. Fresno, CA 93722 www.murach.com murachbooks@murach.com Expanded

More information

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG) Relational Database Languages Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG) Domain relational calculus QBE (used in Access) History of SQL Standards:

More information

Creating QBE Queries in Microsoft SQL Server

Creating QBE Queries in Microsoft SQL Server Creating QBE Queries in Microsoft SQL Server When you ask SQL Server or any other DBMS (including Access) a question about the data in a database, the question is called a query. A query is simply a question

More information

CSC 443 Data Base Management Systems. Basic SQL

CSC 443 Data Base Management Systems. Basic SQL CSC 443 Data Base Management Systems Lecture 6 SQL As A Data Definition Language Basic SQL SQL language Considered one of the major reasons for the commercial success of relational databases SQL Structured

More information

Inquiry Formulas. student guide

Inquiry Formulas. student guide Inquiry Formulas student guide NOTICE This documentation and the Axium software programs may only be used in accordance with the accompanying Ajera License Agreement. You may not use, copy, modify, or

More information

Chapter 5. SQL: Queries, Constraints, Triggers

Chapter 5. SQL: Queries, Constraints, Triggers Chapter 5 SQL: Queries, Constraints, Triggers 1 Overview: aspects of SQL DML: Data Management Language. Pose queries (Ch. 5) and insert, delete, modify rows (Ch. 3) DDL: Data Definition Language. Creation,

More information

Introduction to SQL for Data Scientists

Introduction to SQL for Data Scientists Introduction to SQL for Data Scientists Ben O. Smith College of Business Administration University of Nebraska at Omaha Learning Objectives By the end of this document you will learn: 1. How to perform

More information

Click to create a query in Design View. and click the Query Design button in the Queries group to create a new table in Design View.

Click to create a query in Design View. and click the Query Design button in the Queries group to create a new table in Design View. Microsoft Office Access 2010 Understanding Queries Queries are questions you ask of your database. They allow you to select certain fields out of a table, or pull together data from various related tables

More information

Oracle Database 10g Express

Oracle Database 10g Express Oracle Database 10g Express This tutorial prepares the Oracle Database 10g Express Edition Developer to perform common development and administrative tasks of Oracle Database 10g Express Edition. Objectives

More information

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Example Instances We will use these instances of the Sailors and Reserves relations in our

More information

SQL: Queries, Programming, Triggers

SQL: Queries, Programming, Triggers SQL: Queries, Programming, Triggers Chapter 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 R1 Example Instances We will use these instances of the Sailors and Reserves relations in

More information

Chapter 1 Overview of the SQL Procedure

Chapter 1 Overview of the SQL Procedure Chapter 1 Overview of the SQL Procedure 1.1 Features of PROC SQL...1-3 1.2 Selecting Columns and Rows...1-6 1.3 Presenting and Summarizing Data...1-17 1.4 Joining Tables...1-27 1-2 Chapter 1 Overview of

More information

Section of DBMS Selection & Evaluation Questionnaire

Section of DBMS Selection & Evaluation Questionnaire Section of DBMS Selection & Evaluation Questionnaire Whitemarsh Information Systems Corporation 2008 Althea Lane Bowie, Maryland 20716 Tele: 301-249-1142 Email: mmgorman@wiscorp.com Web: www.wiscorp.com

More information

Performing Queries Using PROC SQL (1)

Performing Queries Using PROC SQL (1) SAS SQL Contents Performing queries using PROC SQL Performing advanced queries using PROC SQL Combining tables horizontally using PROC SQL Combining tables vertically using PROC SQL 2 Performing Queries

More information

SQL Server Database Coding Standards and Guidelines

SQL Server Database Coding Standards and Guidelines SQL Server Database Coding Standards and Guidelines http://www.sqlauthority.com Naming Tables: Stored Procs: Triggers: Indexes: Primary Keys: Foreign Keys: Defaults: Columns: General Rules: Rules: Pascal

More information

Advanced Online Media Dr. Cindy Royal Texas State University - San Marcos School of Journalism and Mass Communication

Advanced Online Media Dr. Cindy Royal Texas State University - San Marcos School of Journalism and Mass Communication Advanced Online Media Dr. Cindy Royal Texas State University - San Marcos School of Journalism and Mass Communication Using SQLite Manager SQL or Structured Query Language is a powerful way to communicate

More information

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur SQL is a standard language for accessing and manipulating databases. What is SQL? SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL is an ANSI (American National

More information

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves Tutorial 5: SQL By Chaofa Gao Tables used in this note: Sailors(sid: integer, sname: string, rating: integer, age: real); Boats(bid: integer, bname: string, color: string); Reserves(sid: integer, bid:

More information

Unit 10: Microsoft Access Queries

Unit 10: Microsoft Access Queries Microsoft Access Queries Unit 10: Microsoft Access Queries Introduction Queries are a fundamental means of accessing and displaying data from tables. Queries used to view, update, and analyze data in different

More information

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema: CS145 Lecture Notes #10 SQL Programming Example schema: CREATE TABLE Student (SID INTEGER PRIMARY KEY, name CHAR(30), age INTEGER, GPA FLOAT); CREATE TABLE Take (SID INTEGER, CID CHAR(10), PRIMARY KEY(SID,

More information

Querying Microsoft SQL Server

Querying Microsoft SQL Server Course 20461C: Querying Microsoft SQL Server Module 1: Introduction to Microsoft SQL Server 2014 This module introduces the SQL Server platform and major tools. It discusses editions, versions, tools used

More information

1 Structured Query Language: Again. 2 Joining Tables

1 Structured Query Language: Again. 2 Joining Tables 1 Structured Query Language: Again So far we ve only seen the basic features of SQL. More often than not, you can get away with just using the basic SELECT, INSERT, UPDATE, or DELETE statements. Sometimes

More information

Writing Control Structures

Writing Control Structures Writing Control Structures Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 5-1 Objectives After completing this lesson, you should be able to do the following: Identify

More information

Course ID#: 1401-801-14-W 35 Hrs. Course Content

Course ID#: 1401-801-14-W 35 Hrs. Course Content Course Content Course Description: This 5-day instructor led course provides students with the technical skills required to write basic Transact- SQL queries for Microsoft SQL Server 2014. This course

More information

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY 2.1 Introduction In this chapter, I am going to introduce Database Management Systems (DBMS) and the Structured Query Language (SQL), its syntax and usage.

More information

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu Introduction to SQL and SQL in R LISA Short Courses Xinran Hu 1 Laboratory for Interdisciplinary Statistical Analysis LISA helps VT researchers benefit from the use of Statistics Collaboration: Visit our

More information

MOC 20461C: Querying Microsoft SQL Server. Course Overview

MOC 20461C: Querying Microsoft SQL Server. Course Overview MOC 20461C: Querying Microsoft SQL Server Course Overview This course provides students with the knowledge and skills to query Microsoft SQL Server. Students will learn about T-SQL querying, SQL Server

More information

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio Querying Microsoft SQL Server 2012 Microsoft Course 10774 This 5-day instructor led course provides students with the technical skills required to write basic Transact-SQL queries for Microsoft SQL Server

More information

Querying Microsoft SQL Server 20461C; 5 days

Querying Microsoft SQL Server 20461C; 5 days Lincoln Land Community College Capital City Training Center 130 West Mason Springfield, IL 62702 217-782-7436 www.llcc.edu/cctc Querying Microsoft SQL Server 20461C; 5 days Course Description This 5-day

More information

www.gr8ambitionz.com

www.gr8ambitionz.com Data Base Management Systems (DBMS) Study Material (Objective Type questions with Answers) Shared by Akhil Arora Powered by www. your A to Z competitive exam guide Database Objective type questions Q.1

More information

Introduction to the Oracle DBMS

Introduction to the Oracle DBMS Introduction to the Oracle DBMS Kristian Torp Department of Computer Science Aalborg University www.cs.aau.dk/ torp torp@cs.aau.dk December 2, 2011 daisy.aau.dk Kristian Torp (Aalborg University) Introduction

More information