Data Manipulation Language (DML)

Similar documents
CSC Web Programming. Introduction to SQL

Database design process

Lab # 6. Data Manipulation Language (DML)

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Database Technology. Topic 3: SQL. Olaf Hartig.

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

ECE 650 Systems Programming & Engineering. Spring 2018

Slides by: Ms. Shree Jaswal

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

SQL STRUCTURED QUERY LANGUAGE

Querying Data with Transact SQL

Database Lab Lab 6 DML part 3

SQL Retrieving Data from Multiple Tables

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Overview Relational data model

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

Basic SQL II. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

SQL Data Query Language

Querying Data with Transact-SQL

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

CS 582 Database Management Systems II

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

SQL. Char (30) can store ram, ramji007 or 80- b

T-SQL Training: T-SQL for SQL Server for Developers

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

Full file at

Sql Server Syllabus. Overview

Relational Database Management Systems for Epidemiologists: SQL Part II

SQL functions fit into two broad categories: Data definition language Data manipulation language

Lab # 4. Data Definition Language (DDL)

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Aggregate Functions. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Dept. Database Lab (ECOM 4113)

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Relational Database Management Systems for Epidemiologists: SQL Part I

Querying Data with Transact-SQL

Unit 1 - Chapter 4,5

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

1) Introduction to SQL

UFCEKG 20 2 : Data, Schemas and Applications

Lecture 6 - More SQL

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

Chapter 6 - Part II The Relational Algebra and Calculus

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Structure Query Language (SQL)

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

The SQL data-definition language (DDL) allows defining :

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 3: Introduction to SQL

CSIE30600 Database Systems Basic SQL 2. Outline

Database Management Systems,

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Lecture 06. Fall 2018 Borough of Manhattan Community College

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

20461: Querying Microsoft SQL Server 2014 Databases

Querying Microsoft SQL Server 2014

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Querying Data with Transact-SQL (20761)

SQL Functions (Single-Row, Aggregate)

Oracle Syllabus Course code-r10605 SQL

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

Lesson 2. Data Manipulation Language

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Chapter 3: Introduction to SQL

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Querying Microsoft SQL Server 2012/2014

Misc. Triggers Views Roles Sequences - Synonyms. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

UNIT-IV (Relational Database Language, PL/SQL)

Fundamentals of Database Systems

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

QUERYING MICROSOFT SQL SERVER COURSE OUTLINE. Course: 20461C; Duration: 5 Days; Instructor-led

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

RESTRICTING AND SORTING DATA

Introduction to SQL Server 2005/2008 and Transact SQL

AVANTUS TRAINING PTE LTD

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 4

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Oracle Database 11g: SQL and PL/SQL Fundamentals

Structured Query Language (SQL)

Relational Database Language

SQL Data Querying and Views

Transcription:

In the name of Allah Islamic University of Gaza Faculty of Engineering Computer Engineering Department ECOM 4113 DataBase Lab Lab # 3 Data Manipulation Language (DML) El-masry 2013

Objective To be familiar with another part of SQL Language that is Data Manipulation Language (DML). INSERTING DATA INSERT INTO command used to insert rows in a table. - Basic Use: INSERT INTO TableName VALUES (Data Values); The data values are listed in the order in which the columns appear in the table, separated by commas. - Specific Columns: INSERT INTO TName (Columns Names) VALUES(Data Values); - Multiple rows in a single command: INSERT INTO TName (Columns Names) VALUES (R1 Values), (R2 Values), (R3 Values); 2

QUERIES The command to retrieve data from a database is called a query. In SQL the SELECT command is used to specify queries. Attribute List : SELECT <attribute list> [AS COL. ALIAS NAME] FROM <Table List> [AS TABLE ALIAS NAME] [ WHERE <condition> ] [ GROUP BY <grouping attributes> [ HAVING <group selection condition> ] ] [ ORDER BY <column name(s)> [ASC DESC] ] It is a list of the attributes we want to retrieve. It can be: * It is a fast alternative to all column name. Q0: Retrieve DNAME, DNUMBER, MGRSSN and MGRSTARTDATE for each Dapartments. Column(s) name(s). If multitable query refers to two or more attributes with the same name, the relation name prefixed to the attribute name and separating the two by a period. Q1: Retrieve the salary for each Employee. 3

DISTINCT <column(s) name(s)> In a table, some of the columns may contain duplicate values. If you want to list only the different (distinct) values in a table. The DISTINCT keyword can be used to return only distinct (different) values. Q2: Retrieve all distinct salary values. function ([ DISTINCT] <column name> * ) Aggregate Functions in SQL Aggregate functions are used to summarize information from multiple tuples into a single-tuple summary. Grouping is used to create subgroups of tuples before summarization. A number of built-in aggregate functions exist: COUNT, SUM, MAX, MIN, and AVG. The COUNT function returns the number of tuples or values as specified in a query. The functions SUM, MAX, MIN, and AVG can be applied to a set or multiset of numeric values and return, respectively, the sum, maximum value, minimum value, and average 4

(mean) of those values. These functions can be used in the SELECT clause or in a HAVING clause (which we introduce later). Q3: Find the sum of the salaries of all employees, the maximum salary, the minimum salary, and the average salary. Q4: Retrieve the total number of employees in the company. Q5: Count the number of distinct salary values in the database. 5

Table List It can be a table or joined table. Joined Tables A joined table is a table derived from two other (real or derived) tables according to the rules of the particular join type. Join Types 1. CROSS JOIN T1 CROSS JOIN T2 T1,T2 The joined table will contain a row for every possible combination of rows from T1 to T2 consisting of all columns in T1 followed by all columns in T2. If the tables have N and M rows respectively, the joined table will have N * M rows. OR 6

2. INNER JOIN For each row R1 of T1, the joined table has a row for each row in T2 that satisfies the join condition with R1. Join Condition It can be: ON boolean_expression T1 [INNER] JOIN T2 Join_Condition The output of JION ON has all columns of the two tables. USING ( join column list ) It takes a comma-separated list of equated column names from the two tables. The output of JOIN USING has one column for each of the equated pairs of input columns, followed by the remaining columns from each table. 7

T1 NATURAL [INNER] JOIN T2 It forms a USING list consisting of all column names that appear in both input tables. As with USING, these columns appear only once in the output table. 3. LEFT OUTER JOIN T1 LEFT [OUTER] JOIN T2 Join_Condition First, an inner join is performed. Then, for each row in T1 that does not satisfy the join condition with any row in T2, a joined row is added with null values in columns of T2. Thus, the joined table always has at least one row for each row in T1. 8

4. RIGHT OUTER JOIN T1 RIGHT [OUTER] JOIN T2 Join_Condition First, an inner join is performed. Then, for each row in T2 that does not satisfy the join condition with any row in T1, a joined row is added with null values in columns of T1. This is the converse of a left join. The result table will always have a row for each row in T2. 5. FULL OUTER JOIN T1 FULL [OUTER] JOIN T2 Join_Condition First, an inner join is performed. Then, for each row in T1 that does not satisfy the join condition with any row in T2, a joined row is added with null values in columns of T2. Also, for each row of T2 that does not satisfy the join condition with any row in T1, a joined row with null values in the columns of T1 is added. 9

AS (alias) You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names. An alias name could be anything, but usually it is short. SELECT COLName AS alias from TableName AS alias WHERE Clause Operators: WHERE column_name operator Value Operator Meaning = Equal <> Not equal < Less than > Greater than <= Less than or equal >= Greater than or equal between Between an inclusive range in One of multi-value Like Pattern matching: %: any number of any characters. _: one character. 10

Q6: Retrieve the birth date and address of the employee(s) whose name is John B. Smith. Q7: Retrieve the name and address of all employees who work for the Research department. OR Q8: For every project located in Stafford, list the project number, the controlling department number, and the department manager s last name, address, and birth date. 11

Q9: For each employee, retrieve the employee s first and last name and the first and last name of his or her immediate supervisor. Q10: Retrieve all employees whose address is in Houston, Texas. Q11: Show the resulting salaries if every employee working on the ProductX project is given a 10 percent raise. 12

Q12: Retrieve all employees in department 5 whose salary is between $30,000 and $40,000. Q13: Find the sum of the salaries of all employees of the Research department, as well as the maximum salary, the minimum salary, and the average salary in this department. Q14: Retrieve the number of employees in the Research department. Is NULL When we want to compare any value with Null or not null, we use is null and is not null respectively. Q15: Retrieve all employees who have no manager. 13

Grouping: The GROUP BY and HAVING Clauses GROUP BY Clause is used to group together those rows in a table that have the same values in all the columns listed in selection list. Group by <grouping attributes> HAVING provides a condition on the groups which necessarily involve an aggregate function. HAVING <group selection condition> Q16: For each department, retrieve the department number, the number of employees in the department, and their average salary. Q17: For each project, retrieve the project number, the project name, and the number of employees who work on that project. 14

Q18: For each project on which more than two employees work, retrieve the project number, the project name, and the number of employees who work on the project. Ordering of Query Results ORDER BY clause allows the user to order the tuples in the result of a query by the values of one or more of the attributes that appear in the query result. The default order is in ascending order of values. ORDER BY <column name(s)> [ASC DESC] Q19: Retrieve a list of employees and the projects they are working on, ordered by department and, within each department, ordered alphabetically by last name, then first name. 15

Updating Data UPDATE command is used to modify attribute values of one or more selected tuples. UPDATE TName SET col1=v1[, col2= ] where selection condition; U1: Change the location and controlling department number of project number 10 to Bellaire and 5, respectively. Deleting Data DELETE command removes tuples from a relation. DELETE FROM TName WHERE selection condition; If you simply write: DELETE FROM TName; Then all rows in the table will be deleted! 16

INSERT INTO WITH SELECT A variation of the INSERT command inserts multiple tuples into a relation in conjunction with creating the relation and loading it with the result of a query. 17

EXERCISE It will be in the lab. Prepare yourself well to get bonus. 18