1 Information Retrieval and Use (IRU) CE00873-2 An Introduction To SQL Part 1.

Slides:



Advertisements
Similar presentations
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Advertisements

Virtual training week 4 structured query language (SQL)
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
Introduction to Structured Query Language (SQL)
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
ASP.NET Programming with C# and SQL Server First Edition
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL (DDL & DML Commands)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
Nested Queries (Sub Queries) A nested query is a form of a SELECT command that appears inside another SQL statement. It is also termed as subquery. The.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
An Introduction To SQL - Part 1 (Special thanks to Geoff Leese)
SQL.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
SQL FUNDAMENTALS SQL ( Structured Query Language )
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
SQL. Relating Multiple Tables Relational Database Terminology Row PK Column FK Field NULL.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
Copyright س Oracle Corporation, All rights reserved. I Introduction.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
11-1 © Prentice Hall, 2004 Chapter 11: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
RELATSIOONILISED ANDMEBAASID (algus , ORACLE materjalid)
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Writing Correlated Subqueries
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 2 Views.
Chapter 2 Views.
Writing Basic SQL Statements
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
IST 318 Database Administration
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Presentation transcript:

1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1

2 Outline of Lecture/Tutorial approach n The first two weeks will concentrate on the following: n Select Statements n The Where Clause n Manipulating data using the emp and dept tables using Oracle SQL+ so that you get a feel for the SQL language. n Creating tables, populating them with data and manipulating the data. n The tutorials will allow you to work through a series of questions to reinforce the lecture material. You will find it useful to keep your lecture material with you during the tutorials.

3 The Basics: SQL Command Set n Data Manipulation Language (DML) ä Insert allows you to enter new rows ä Update allows you to change existing rows ä Delete remove unwanted rows n Select is used to retrieve data from the database. n Data Description Language (DDL) ä Create create objects ä Alter structurally change objects ä Drop drop objects within the database (Objects are tables, views, indexes etc.)

4 The SQL Command Set continued n Data Control Language (DCL) ä Grant specify access rights on objects ä Revoke remove access rights These statements are NOT case-sensitive Each of the statements should be terminated with a semicolon ‘;’ at the end of the last line, OR a slash can be used on the following line.

5 The Select Statement n This is the most commonly used statement, to query database information. n The informal syntax is a follows: SELECT column_name(s) FROM table_name(s) WHERE constraint(s); ‘*’ asks for all the columns to be returned.

6 The Select Statement: data retrieval. n The top line ‘SELECT’ describes the column name(s) from which you are attempting to build the query. n The second line ‘FROM’ lists the tables or relations that contain the data necessary to service the query n The third line, the ‘WHERE’ constraint is optional. The WHERE clause allows you to specify the tuples (rows) to be returned.

7 The Select Statement: Examples n The basic statement: n If we wish to display all of the tuples (rows) of a given relation (i.e. table): SELECT * FROM EMP; (; end of statement to be executed) ä We can constrain this by requesting only those employee’s with the name ‘Allen’ SELECT * FROM EMP WHERE ENAME = ‘ALLEN’; (this is the constraint) ä ‘Allen’ is enclosed in quotation marks as we are searching on a column data type which is a string

8 Select Statement Examples n If we want to search on numeric columns: –SELECT * –FROM DEPT –WHERE DEPTNO = 10; –SELECT * –FROM EMP –WHERE EMPNO = 7654;

9 The ‘Where’ Clause n This clause is used to restrict the output to show a subset of the rows from a table. n It must be placed after the FROM clause n The WHERE clause has the following format: n WHERE Operators may be: ä Comparative Operators ä = != = <> and ä Logical Operators AND, OR, NOT SELECT * FROM EMP WHERE EMPNO = 7521 AND ENAME = ‘WARD’;

10 Where Clause Example n If we said: SELECT * FROM EMP WHERE EMPNO = 7369 AND ENAME = ‘WARD’; We would return an empty table as there are no tuples (rows) that satisfy both of these constraints. However if we said: SELECT * FROM EMP WHERE EMPNO = 7876 OR ENAME = ‘KING’ Two tuples would be returned as these would satisfy one or other of the constraints.

11 Select statement: retrieving attributes n You can retrieve attribute values by specifying columns: The command is –SELECT COLUMN_NAME (S) –FROM TABLE_NAME (S); –SELECT ENAME –FROM EMP; –Will return one column, and the values from the ename column (14 rows). –SELECT EMPNO,ENAME,JOB –FROM EMP; –Will return three columns, in that order (14 rows)

12 Where clause: more constraints n Row and Columns constraints can be set as follows: SELECT EMPNO, ENAME FROM EMP WHERE SAL > 2000; Returns only those columns empno and ename from the emp table and only those rows with a balance greater than SELECT EMPNO, ENAME, JOB, SAL FROM EMP WHERE SAL BETWEEN 1500 AND 3000; Displays all those employees who earn between 1500 and 3000 (four columns and eight rows in this case)

13 Where clause: The ‘Like’ operator n If we want to search for employees whose name begins with the letter ‘A’ We can say: SELECT EMPNO, ENAME, JOB FROM EMP WHERE ENAME LIKE ‘A%’; This will display two tuples (rows) i.e. all those employees whose names begin with A and three columns. The ‘%’ is a wildcard operator.

14 Where clause: ‘Not In’ operator n You can use the ‘NOT IN’ operator as follows: SELECT ENAME, JOB, SAL FROM EMP WHERE JOB NOT IN (‘SALESMAN’, ‘MANAGER’); This will return those employees who are not salesmen or managers. You can also use the operator !=

15 Where clause: multiple conditions n If you want to find all the clerks in department 30 who earn less than $1000 you can say: SELECT ENAME,JOB, SAL FROM EMP WHERE DEPTNO = 30 AND JOB = ‘CLERK’ AND SAL < 1000; This will return 1 row for James, a clerk who earns $950.

16 Joins n Oracle provides you with the capability to gather and manipulate data over several tables using the JOIN feature. n A join is one of the fundamental operations in relational algebra. n The basic Structure is: ä SELECT table1.column1, table1.column2, table2.column1 ä FROM table1, table2, tablen ä WHERE join criteria;

17 Joining Tables: Example ä We want to know where ALLEN works. The EMP table does not hold the location details but the DEPT table does. SELECT ename, loc FROM emp,dept WHERE emp.deptno = dept.deptno; AND ename = ‘ALLEN’ We need to distinguish between the deptno in the two tables therefore they are prefixed with the table name. Here we have an equi-join in the WHERE clause.

18 Joining Tables:Use of Abbreviations/aliases SELECT d.*, ename, job FROM emp e, dept d WHERE e.deptno = d.deptno AND e.deptno = (30,40) ORDER BY e.deptno; The alias d.* will retrieve all the column names in the DEPT Table. We use aliases to identify which column name belong to which table as some column names will exist in more than one table for example, deptno.

19 Joining Tables: Outer Join n Normally when tables are joined we use primary/foreign keys which are generally defined as NOT NULL, however to join non key elements we may use an OUTER JOIN. ä i.e Department 40 in table DEPT has no matching employees in table EMP so normal join queries would not retrieve department 40. ä The symbol for outer joins is an (+) –SELECT DEPT.DEPTNO, DNAME, JOB, ENAME –FROM DEPT, EMP –WHERE DEPT.DEPTNO = EMP.DEPTNO(+) –AND DEPT.DEPTNO = (30,40) –ORDER BY DEPT.DEPTNO;

20 Views n A view is a type of window that allows you to view selected portions of data. n Advantages: n Security: a secure table may contain sensitive or confidential information that prevents it from general circulation. You can deny access to the emp table itself but instead provide access to a VIEW without the salary or commission details. n A view can be constructed on a need to know basis. n Convenience: instead of using complex queries to get a perspective on your data, you can create a view which allows you to obtain the same information with a simple query.

21 Views n No additional memory is required for a view as the view is created from the base tables every time the view is called. ä CREATE VIEW view_name AS ä (SELECT columns FROM table_name WHERE row_selector); CREATE VIEW EMP10 AS SELECT EMPNO, ENAME. JOB FROM EMP WHERE DEPTNO = 10; A message will be displayed ‘view created’ you can check your view by: SELECT * FROM EMP10;

22 Views n You can add, delete or update data in a view. UPDATE emp10 SET job = ‘CLERK’ Where ename = ‘MILLER’; n Delete, rename or drop a view. DELETE FROM emp10 WHERE ename = ‘Wilson’; RENAME emp10 to emp11; DROP VIEW emp11;

23 Queries: Nested Sub Queries n When one of the conditions in a WHERE clause is a query itself it is known as a nested sub-query. It is used for step- wise processing. SELECT ename, job FROM emp WHERE job = (SELECT job FROM emp WHERE ename = ‘JONES’) –The sub query is enclosed in brackets. –The values compared across the outer query and the subquery must be of the same datatype. –Nested query is performed in step 1 and its result is used in step 2 with the outer query.

24 Nested Sub Queries n Example: Find the person who earns the highest salary: ä Step 1: find the maximum salary ä Step 2: Find the person whose salary is equal to the maximum salary ä SELECT ename, sal ä FROM emp ä WHERE sal = (SELECT MAX(sal) FROM emp);

25 Next week n More DML ä – additional SELECT clauses ä Insert, Alter and Delete n DDL – CREATing, ALTERing and DROPping objects

26 Useful Texts n Rolland, F.D (1997), Essence of Databases (Essence of Computing), Prentice-Hall. ISBN n Date C.J. (2000); An Introduction to Database Systems, Addison Wesley, (or any earlier editions) n Patrick J.P. (2002), SQL Fundamentals (2 nd edition), Prentice Hall, ISBN n SQL at w3schools (click to follow the link) SQL at w3schools (click to follow the link)