Advanced SQL Advanced SQL Week 8 Comparison Operators

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

Introduction to Structured Query Language (SQL)
Virtual training week 4 structured query language (SQL)
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
S511 Session 10, IU-SLIS 1 Structured Query Language.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Introduction to SQL J.-S. Chou Assistant Professor.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
Chapter 7 Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Chapter 5 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
Database Systems: Design, Implementation, and Management Tenth Edition
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 7 Introduction to Structured Query Language (SQL)
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 6 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
7 7 SQL Data Modification 4Spread throughout chapter 7.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.
SQL – Structured Query Language
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Query Getting to the data ……..
Relational Database Design
Writing Basic SQL SELECT Statements
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Structured Query Language (SQL) How to build something useful
Introduction to Oracle9i: SQL
CSCI 2141 – Intro to Database Systems
Session #, Speaker Name Indexing Chapter 8 11/19/2018.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 7 Introduction to Structured Query Language (SQL)
HAVING,INDEX,COMMIT & ROLLBACK
COP 4610L: Applications in the Enterprise Spring 2005
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Structured Query Language – The Basics
COP 4610L: Applications in the Enterprise Spring 2006
Presentation transcript:

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

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.

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);

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

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’;

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;

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

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=?

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

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.

Special Operators SELECT * FROM product WHERE p_price BETWEEN 50.00 AND 100.00; 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);

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

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'

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

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

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

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;

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

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;

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;

practice Check the link bellow: https://nam.inna.is/api/Attachment/DownloadFile/175995/455172