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

Similar documents
Objectives. After completing this lesson, you should be able to do the following:

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

GIFT Department of Computing Science. CS-217: Database Systems. Lab-4 Manual. Reporting Aggregated Data using Group Functions

Database implementation Further SQL

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

CS2 Current Technologies Lecture 2: SQL Programming Basics

CSC Web Programming. Introduction to SQL

SQL Structured Query Language Introduction

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

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

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

Visit for more.

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

Chapter 16: Advanced MySQL- Grouping Records and Joining Tables. Informatics Practices Class XII. By- Rajesh Kumar Mishra

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

Data Manipulation Language (DML)

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

1 SQL Structured Query Language

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

1 SQL Structured Query Language

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

RESTRICTING AND SORTING DATA

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

Databases - 5. Problems with the relational model Functions and sub-queries

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

Sample Question Paper

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

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

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

Pivot Tables Motivation (1)

Intermediate SQL: Aggregated Data, Joins and Set Operators

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

Real-World Performance Training SQL Introduction

Database Management Systems,

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

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

NULLs & Outer Joins. Objectives of the Lecture :

Subquery: There are basically three types of subqueries are:

Downloaded from

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

CS 582 Database Management Systems II

5 Integrity Constraints and Triggers

Full file at

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

COMP 244 DATABASE CONCEPTS & APPLICATIONS

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

Relational Database Management Systems for Epidemiologists: SQL Part I

Slicing and Dicing Data in CF and SQL: Part 1

Database Management Systems,

STIDistrict Query (Basic)

CS2 Current Technologies Lecture 3: SQL - Joins and Subqueries

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

Unit Assessment Guide

SQL STRUCTURED QUERY LANGUAGE

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

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

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

Lecture 6 - More SQL

Name: Sample Answers Q.2(c) 7 7

Tables From Existing Tables

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

Chapter 3: Introduction to SQL

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

12. MS Access Tables, Relationships, and Queries

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

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

SQL Data Query Language

CS44800 Practice Project SQL Spring 2019

Creating Other Schema Objects

CGS 3066: Spring 2017 SQL Reference

SQL Retrieving Data from Multiple Tables

Lesson 2. Data Manipulation Language

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Database 2: Slicing and Dicing Data in CF and SQL

Chapter 3: Introduction to SQL

Exact Numeric Data Types

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

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

Assignment 5: SQL II Solution

Outline. Introduction to SQL. What happens when you run an SQL query? There are 6 possible clauses in a select statement. Tara Murphy and James Curran

Structure Query Language (SQL)

Introduction to SQL. Tara Murphy and James Curran. 15th April, 2009

Microsoft MOS- Using Microsoft Office Access Download Full Version :

Handout 9 CS-605 Spring 18 Page 1 of 8. Handout 9. SQL Select -- Multi Table Queries. Joins and Nested Subqueries.

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

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

Import and Browse. Review data. bp_stages is a chart based on a graphic

Further GroupBy & Extend Operations

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

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

Tables and Volatile Tables

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

CSE 530A SQL. Washington University Fall 2013

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

SQL (Structured Query Language)

SQL Functions (Single-Row, Aggregate)

Queries. Chapter 6. In This Chapter. c SELECT Statement: Its Clauses and Functions. c Join Operator c Correlated Subqueries c Table Expressions

3. Getting data out: database queries. Querying

Natural-Join Operation

Transcription:

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 INSERT INTO DEPT VALUES(4, 'Prog','MO'); The result after executing the query is : 2. Write an SQL statement to insert a new row in table Dept with the following values: 5, Reg. Notice that location value is not given, so we will put NULL instead. INSERT INTO Dept VALUES(5, 'Reg',NULL); The Result is: 3. Write an SQL statement to Update Dept with id 2 to 20. UPDATE Dept SET id = 20 id = 2;

The result is: 4. Update the location for dept 5 to MA UPDATE dept SET location = 'MA' id = 5; The result : 5. Delete the dept with number 5; The result : DELETE FROM Dept id = 5; 6. Write an SQL statement to show all rows in Dept including all fields: SELECT * FROM Dept; or

SELECT ID, NAME, Location FROM Dept; Here, * means that I want to include all fields in my query/ 7. Write an SQL statement to show all departments with location Manama. We need to show all fields. SELECT * FROM Dept Location = 'Manama'; The result when executing the query is: 8. Let us add a new field in Table Dept. and name it as Budget of type Number ; its value as follows Now, we need to show departments with location in Manama and budget more than 800.Just we want to show the ID and Name. SELECT ID, Name FROM Dept Location ='Manama' AND Budget >800; The result when executing the query is: Change the Budget in the query above to 1500 and execute the query, How any records did you get? 9. Now we want to write an SQL query to show departments with location in Manama or budget more than 800.

ID, Name Dept ='Manama' or 800; 10. Write an SQL query to Update the Budgets of all departments by adding 1000 to each budget. UPDATE Dept SET Budget = Budget +1000; Here, Budget for all rows will be selected. The following screen shot shows the Budgets after the update: 11. Update dept with ID 3 to a new ID 30. UPDATE = ID = 3; The following shows the changes occurred in the Dept table after executing query 12. Write an SQL statement to delete all rows in Dept DELETE FROM Dept;

Don t execute this query because it will delete all Dept rows. 13. Computed columns: assume that we want to multiply each Budget by 3 without updating the Budget; just viewing. SELECT ID, Name, Location, Budget, Budget*3 FROM Dept; The result when executing the query is : as you see the result of multiplying Budget by 3 was given in a field name given by Access Expr1004. As you see the name Expr1004 is not a good name so the solution to use alias, alias is an alternative name given to a column in any SQL statement. SELECT ID, Name, Location, Budget, Budget*3 AS NewBudget FROM Dept; Note: NewBudget will not stored as a field in the Dept table. 14. The logical operator NOT Assume that we want to show information for all departments except Dept number 4. SELECT ID, Name, Location, Budget FROM Dept NOT (ID=4); The result when executing the query is :

The same above query can be done using <> operator SELECT ID, Name, Location, Budget FROM Dept ID<>4; 15. Special operators: The following operators are used in conjunction with the clause. a. BETWEEN to check whether a field value is within a range Example: Write an SQL statement to show departments with Budget between 1500 and 4000. SELECT ID, Name, Budget FROM Dept Budget BETWEEN 1500 AND 4000; The result when executing the query: b. IS NULL Used to check whether a field value is null. Example: Write an SQL statement to show departments with no Budget SELECT ID, Name, Budget FROM Dept Budget IS NULL; When executing the query, the following result comes: The reason is that all departments have budgets

Exercise: Insert a new row in Dept with values 50 for ID and Aviation for Name, leave Budget Null (empty). Remember that 0 is a value it is not NULL. Use an INSERT query to accomplish that. When you have done that, you execute the above query SELECT ID, Name, Budget FROM Dept Budget IS NULL; Did you get any records? Note: The opposite of NULL is NOT NULL. c. LIKE Used to check whether a field value matches a give string pattern. Example: Write an SQL statement to list all dept s that their location starts with M. SELECT * FROM Dept Location like 'M*'; The following result shows when executing the query: d. IN-Used to check whether a field value matches any value within a value list. Example: Write an SQL statement to show information for Dept s 1, 4, 20. SELECT * FROM Dept ID IN (1,4,20); The following result shows when executing the query:

Note: IN operator can be done using the Or operator SELECT * FROM Dept ID = 1 OR ID = 4 OR ID = 20;

ORDERING A LISTING The ORDER BY clause is used to list a query result in specific order; this order could be ascending (ASC) or descending (DESC). Syntax: SELECT field1, field2, field3, FROM table(s) condition ORDER BY field1, field2,.. [ASC DESC]; The default order is ascending means if we didn t mention ASC or DESC result will be sorted in ascending order according to the field(s) mentioned. Example(1): Write an SQL statement to show information for Dept s 1, 4, 20. SELECT * FROM Dept ID IN (1,4,20); The following result shows when executing the query: From the result you cannot till that it is any order. So, let us adjust the above query as follows: SELECT * FROM Dept ID IN (1,4,20) ORDER BY ID; Run the query:

It is clear that data returned is sorted by ID in ascending order. Note: as I mentioned above that ascending is the default. So if we added ASC to the above query we still get the same result. SELECT * FROM Dept ID IN (1,4,20) ORDER BY ID ASC; Exercise: change the data to be sorted by ID in descending order. SELECT * FROM Dept ID IN (1,4,20) ORDER BY ID DESC; The result when running the query is: Note: you can sort results using more than one field. Now, we want to introduce a new table called Employee in order to finish the rest of the chapter:

LISTING UNIQUE VALUES DISTINCT: is a clause is used to produce a list of only values that are different from one another. Example: how many different deptno s can you get from the Employee table? Assume that we issued the SQL statement : SELECT deptno FROM Employee; You get, as you see the there are all deptno s are duplicated: 20 3 times, 4 twice, 1 twice. Now let us re-write the above query by adding DISTINCT before deptno as: SELECT DISTINCT deptno FROM Employee; You get:

Aggregate Functions SQL provides functions to perform mathematical summaries such as counting the number of rows, finding the minimum or maximum values for a field, summing the values in a field, and averaging the values in a field. The following table shows basic SQL aggregate functions FUNCTION COUNT MIN MAX SUM AVG OUTPUT The number of rows containing non-null values The minimum attribute value encountered in a given column The maximum attribute value encountered in a given column The sum of all values for a given column The arithmetic mean (average) for a specified column Write an SQL statement to find the number of employees in table Employee? SELECT COUNT(empno) AS N_Employee FROM Employee; Executing this query will yield 8 rows. Let us do the COUNT on deptno as follows: SELECT COUNT(deptno) AS N_Employee FROM Employee; How many records are retrieved? Note: The syntax COUNT(*) returns the number of total rows returned by the query, including the rows that contain nulls. SELECT COUNT(*) AS N_Employee FROM Employee;

Write a query to find the maximum, minimum, average of all salaries. SELECT MAX(sal) as Highest, MIN(sal) as lowest, AVG(sal) as Average FROM Employee; Now, assume that we want to find who has the maximum salary? To answer his question we need to use what is called a sub-query SELECT empno, ename, sal FROM Employee sal = (SELECT MAX(sal) FROM Employee); MS-ACCESS, first executes the query to the right of the equal sign(=) and then it will be an input for the value of sal in the condition. Exercise Write an SQL statement to find the employee with the minimum salary GROUPING DATA Assume we want to find the total salaries paid for each department. We use the GROUP BY clause. The GROUP BY clause is valid only in conjunction with one of the SQL aggregate functions, such as COUNT, MIN, MAX, and SUM. Syntax: SELECT field(s) FROM table(s) field(s)

GROUP BY field(s); SELECT deptno, SUM(sal) as TotalSal FROM Employee GROUP BY deptno; Note: The GROUP BY clause s columnlist(field list) must include non-aggregate function columns specified in the SELECT s columnlist. Joining Database Tables Assume that you want to join the two tables Department and Employee As you see from the diagram, the deptno is the foreign key in the Employee table and the primary key in the Department table, the link established on deptno. Assume that the following data are in both tables: Department

Employee The join query is: SELECT Department.deptno, empno, Dept_Name, ename FROM Department INNER JOIN Employee ON Department.deptno = Employee.deptno; The result when executing the query is: From the result, you can note the following: 1. deptno 30 is not found in the result, why? 2. data for employee with number 129 is not found? Whay The same above query can be done using the query: SELECT Department.deptno, empno, Dept_Name, ename FROM Department, Employee Department.deptno = Employee.deptno;

Tip: start a new query as you did before then insert the Department and Employee tables as shown below: Write click and choose SQL view you get SQL code which is generated by MS-ACCESS: SELECT FROM Department INNER JOIN Employee ON Department.deptno = Employee.deptno; Here, what you have to do is write down or click what fields to be shown Left and Righ Joins Start a new query and add the Department and Employee tables to the query:

Link Double click on the link the join Properties window appears: Here you have three choices 1. if you select choice 1 then you get the matching rows between the two tables which is inner join that we performed before: 22. as choice 1 is selected click OK then add the fileds that you want to show in the query result as shown below:

The SQL code is: SELECT Department.deptno, Dept_Name, empno,ename FROM Department INNER JOIN Employee ON Department.deptno = Employee.deptno; The result when executing the query is : 23. Sart a new query and insert the two tables as in above and double click on the link and select the second choice:

Here the result will show all Deprtment records whether or not they match with records in Employee table. The SQL code is: SELECT Department.Dept_id, Employee.empno, Department.Dept_Name, Employee.ename FROM Department LEFT JOIN Employee ON Department.Dept_id = Employee.deptno; The result is:

Note that dept id 40 is shown even there is employees working for this department. 24. Do as in the above and select the the third choice: Assume we have an employee with no deptno as shown below in the Employee table: The SQL code is: SELECT Department.Dept_id, Employee.empno, Department.Dept_Name, Employee.ename

FROM Department RIGHT JOIN Employee ON Department.Dept_id = Employee.deptno; The result is : Note that employee sameeh with no Deptid.