Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced SQL Advanced SQL Week 8 Comparison Operators

Similar presentations


Presentation on theme: "Advanced SQL Advanced SQL Week 8 Comparison Operators"— Presentation transcript:

1 Advanced SQL Advanced SQL Week 8 Comparison Operators
computed columns and column aliases Arithmetic Operators Logical Operators Special Operators SQL SELECT statement SQL UPDATE statement SQL DELETE Statement SQL INSERT Statement

2 Advanced SQL Download the the database script Sale Database, located in inna => efni SQL Meterial. Create database name it YourKenetala_sale Open with wordPad document and edit the file and save as .SQL Import the file to phpmyadmin.

3 SQL INDEXES Indexes are used to improve the efficiency of searches and to avoid duplicate column values. When you declare a primary key, the DBMS automatically creates a unique index. CREATE INDEX <index_name> ON <table_name> (<column1, column2>) Example: CREATE INDEX p_indatex ON product(p_indate);

4 Comparison Operators = equal < less than
<= less than or equal to > Greater than >= Greater than or equal to <> Or != Not equal to

5 Comparison operators examples
SELECT p_descript, p_indate, p_price, v_code FROM product WHERE v_code <> 21344; SELECT p_descript, p_qoh, p_min, p_price WHERE p_price <= 10; SELECT p_code, p_qoh, p_min, p_price WHERE p_code < ’1558-QW1’;

6 Computed columns and aliases
Using computed colums and column aliases SELECT p_descript, p_qoh, p_price, p_qoh * p_price FROM product; SELECT p_descript, p_qoh, p_price, p_qoh * p_price AS totalValue SELECT p_code, p_indate, DATE() - 90 AS CUTDATE FROM product WHERE p_indate <= DATE() – 90;

7 Arithmetic Operators Arithmetic Operator Description + Add - Subtract
* Multiply / Divide ^ Raise to the power of

8 Rules of precedence 1- perform operations within parentheses
2- Perform power operations 3- Perform multiplications and divisions 4- Perform addition and subtractions 8+2*5=? (8+2)*5=? 4+5^2*3=? (4+5)^2 *3=?

9 Logical Operators AND, OR, and NOT
SELECT p_descript, p_indate, p_price, v_code FROM product WHERE v_code = OR v_code = 24288; WHERE p_price < 50 AND p_indate > ’15-Jan-2007’; SELECT * FROM product WHERE v_code <>/!= 21344

10 Special Operators BETWEEN: Used to check whether an attribute value within a range IS NULL: Used to check whether an attribute value is null LIKE: Used to check whether an attribute value matches a given string pattern IN: Used to check whether an attribute value matches any value within a value list. EXISTS: Used to check whether a subquery returns any rows.

11 Special Operators SELECT * FROM product
WHERE p_price BETWEEN AND ; SELECT p_code, p_descript, v_code FROM product WHERE v_code IS NULL; SELECT v_name, v_code, v_areacode, v_phone FROM vendor WHERE v_contact LIKE ‘Smith%’; SELECT * FROM product WHERE v_code IN (21344, 24288);

12 Special Operators SELECT * FROM vendor
WHERE EXISTS (SELECT * FROM product WHERE p_qoh < p_min * 2);

13 UPDATE UPDATE PRODUCT SET P_INDATE = '18-JAN-2007'
WHERE P_CODE = '13-Q2/P2‘ SET P_INDATE = '18-JAN-2007', P_PRICE = 17.99, P_MIN = 10 WHERE P_CODE = '13-Q2/P2'

14 DELETE Copy product table name it P Copy vendor table name it V
DELETE FROM PRODUCT WHERE P_CODE = 'BRT-345‘; WHERE P_MIN = 5; DELETE FROM PRODUCT //Delete all rows from product table DELETE FROM VENDOR //Delete all rows from vendor table

15 INSERT INSERT INTO PRODUCT
VALUES ('11QER/31','Power painter, 15 psi., 3-nozzle',' ',8,5,109.99,0.00,21225); Insert with null attributes VALUES ('BRT-345','Titanium drill bit',' ',75,10,4.50,0.06, NULL) Insert with optional attributes INSERT INTO PRODUCT(P_CODE, P_DESCRIPT) VALUES ('BRT-345','Titanium drill bit')

16 INSERT Inserting rows with SELECT subquery
INSERT INTO VENDOR SELECT * FROM V INSERT INTO PRODUCT SELECT * FROM P SELECT * FROM VENDOR SELECT * FROM PRODUCT

17 COMMIT, ROLLBACK DELETE Deletes one or more rows from a table
COMMIT Permanently saves data changes ROLLBACK Restores data to their original values The Correct way to do that: SET autocommit=0; START TRANSACTION; Your Query here. ROLLBACK;

18 COMMIT, ROLLBACK you have ordered 20 units of product 2232/QWE. When the 20 units arrive, you’ll want to add them to inventory, using: UPDATE PRODUCT SET P_QOH = P_QOH + 20 WHERE P_CODE = '2232/QWE'; If you want to add 10 percent to the price for all products that have current prices below $50, you can use: SET P_PRICE = P_PRICE * 1.10 WHERE P_PRICE < 50.00; ROLLBACK command to undo the changes made by the last two UPDATE statements

19 GROUP BY GROUP BY clause is valid only when used in conjunction with one of the SQL aggregate functions SELECT V_CODE, P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT GROUP BY V_CODE;

20 Creating views A view is a virtual table based on a SELECT query. The query can contain columns, computed columns, aliases, and aggregate functions from one or more tables. Syntax: CREATE VIEW viewname AS SELECT query Example: CREATE VIEW PROD_STATS AS SELECT V_CODE, SUM(P_QOH*P_PRICE) AS TOTCOST, MAX(P_QOH) AS MAXQTY, MIN(P_QOH) AS MINQTY, AVG(P_QOH) AS AVGQTY FROM PRODUCT GROUP BY V_CODE;

21 practice Check the link bellow:


Download ppt "Advanced SQL Advanced SQL Week 8 Comparison Operators"

Similar presentations


Ads by Google