Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Self-joins,

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

Database Fundamentals
Simplified Management using the Enterprise Policy Management Framework
Chapter 5 Normalization of Database Tables
© Abdou Illia MIS Spring 2014
Displaying Data from Multiple Tables
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.
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
Creating Databases and E/R Diagrams with SQL Server Management Studio Svetlin Nakov Telerik Corporation
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Physical.
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Microsoft Enterprise Consortium Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Database Fundamentals Defining.
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
6.830/6.814 Lecture 5 Database Internals Continued September 17, 2014.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Create Student-Team.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Aggregate.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals The Data.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Evaluating.
Mid-term Exam Answer Keys Week 7 Spring I. Multiple Choices Questions Each may have more than one answer (6 x 4 = 24 points total)
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Greenhouse Database.
Database – Part 2a Dr. V.T. Raja Oregon State University.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Type II (Correlated)
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Using two.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SQL Server.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
PLUG IT IN 3 Fundamentals of Relational Database Operations.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Test.
SQL Structured Query Language Programming Course.
DAY 12: DATABASE CONCEPT Tazin Afrin September 26,
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SELECT … FROM.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Nested aggregate.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Aggregating.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Recap of SQL Lab no 8 Advance Database Management System.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SELECT … FROM.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Physical.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Logical operators.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL In-Line Subquery.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Equi-Join One-sided.
While you are waiting for class to start... (1)Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab4.sql”. It is located on the.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Set Operators:
Engineering Project Management Database David H. Williams CS 610.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals WHERE clause.
DATA MODELING AND DATABASE DESIGN DATA MODELING AND DATABASE DESIGN Part 2.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL The relational.
SSMS SQL Server Management System. SQL Server Microsoft SQL Server is a Relational Database Management System (RDBMS) Relational Database Management System.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
For more course tutorials visit CIS 336 All iLabs Week 1 to Week 7 Devry University CIS 336: All iLabs Week 1 to Week 7: Devry University.
SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS580 Advanced Database Topics
CIS 336 strCompetitive Success/tutorialrank.com
CIS 336 str Education for Service-- tutorialrank.com.
Generalization.
Microsoft Enterprise Consortium Type I Subquery
Database Fundamentals
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Using Subqueries to Solve Queries
SQL Server Fundamentals for Beginners
Presentation transcript:

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Self-joins, 2 joins between 2 tables, and table aliases 1 Microsoft Enterprise Consortium Microsoft Enterprise Consortium: Microsoft Faculty Connection/Faculty Resource Center

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What you’ll need … Log in to MEC for this lesson and into MSSMS (Microsoft SQL Server Management Studio). ◦Be sure to select your account ID under Database in the Object Explorer pane, similar to the example shown here. If there is something besides the self-join and table alias topics that you’re not familiar with in this presentation, please review earlier lessons in this SQL Fundamental series. 2

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Self-joins A unary relationship occurs when an entity (table) is related to itself. This is not that unusual in a database, by the way. For this presentation we’ll use the Student-Teams database and we’ll also use a simple one-table example created just for this lesson. EMPLOYEE: The ERD shown here shows that the employee table has a one-to-many relationship with itself. 3

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium EMPLOYEE – A unary relationship Why is the employee related to itself? ◦An employee may report to another employee (supervisor). ◦An employee may supervise zero to many employee (subordinates). In the EMPLOYEE table example, emp_ID is the primary key. Emp_Supv is the foreign key (this is the supervisor’s employee ID). SELF-JOIN: If we want a list of employees and the names of their supervisors, we’ll have to JOIN the EMPLOYEE table to itself to get this list. 4

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium EMPLOYEE – Create table & insert data To prepare for the self-join, create the employee table and insert data. (The SQL script is provided to instructors.) Insert data. 5 CREATE TABLE dbo.employee( emp_ID varchar(4) NOT NULL, emp_Fname varchar(10) NULL, emp_Lname varchar(10) NULL, emp_Supv varchar(4) NULL, CONSTRAINT PK_employee PRIMARY KEY(emp_ID), CONSTRAINT FK_supervisor FOREIGN KEY(emp_Supv) REFERENCES employee(emp_ID)); insert into employee (emp_id, emp_fname, emp_lname, emp_supv) values ('1050', 'Carlo', 'Mora', NULL); insert into employee (emp_id, emp_fname, emp_lname, emp_supv) values ('1062', 'Robert', 'Block', 1050); insert into employee (emp_id, emp_fname, emp_lname, emp_supv) values ('1063', 'Teresa', 'Roberts', 1062); insert into employee (emp_id, emp_fname, emp_lname, emp_supv) values ('1077', 'Carla', 'Stevens', 1050); insert into employee (emp_id, emp_fname, emp_lname, emp_supv) values ('1080', 'Wilma', 'Washington', 1050); insert into employee (emp_id, emp_fname, emp_lname, emp_supv) values ('1081', 'Rory', 'Block', 1062);

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium EMPLOYEE data If you look at the employee data, you can figure out that Robert Block’s supervisor is Carlo Mora. Carla Stevens and Wilma Washington also report to Carlo Mora. Select * from employee; How can we get the list of employees and their supervisors? 6

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Using EMPLOYEE twice in a query. We will use the EMPLOYEE table twice in the query. In order to do this we have to rename the table—give it an alias. When we list the table in the FROM clause the table name is immediately followed by the table alias. We’ll use sub (subordinate) and supv (supervisor). select... from employee sub, employee supv Where... You should decide on the aliases before typing the SELECT clause because you’ll use them in the SELECT clause. select sub.emp_id as "Sub ID", sub.emp_fname + ' ' + sub.emp_lname as "Subordinate", supv.emp_id as "Supv ID", supv.emp_fname + ' ' + supv.emp_lname as "Supervisor" from employee sub, employee supv where sub.emp_supv = supv.emp_id; 7

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Self-join output: List of employees and their supervisors. select sub.emp_id as "Sub ID", sub.emp_fname + ' ' + sub.emp_lname as "Subordinate", supv.emp_id as "Supv ID", supv.emp_fname + ' ' + supv.emp_lname as "Supervisor" from employee sub, employee supv where sub.emp_supv = supv.emp_id; 8

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Another need for table aliases: Two joins between two tables The Student-Teams database has two tables with two relationships. To get the names of evaluatees and evaluators, we’ll need to use the STUDENTS table twice—one for each relationship to EVALUATIONS. 9 TEAMSSTUDENTS EVALUATIONSEVAL_ITEMS_SCORES EVAL_ITEMS EvaluateeEvaluator

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Another need for table aliases: Two joins between two tables /* List evaluatees and evaluators. Show the evaluation ID, student IDs and full names. */ We’ll use EVALUATEE and EVALUATOR as the table aliases for the STUDENTS table. select eval_ID, evaluatee.stdid as "Evaluatee ID", evaluatee.stdfname + ' ' + evaluatee.stdlname as "Evaluatee ID", evaluator.stdid as "Evaluator ID", evaluator.stdfname + ' ' + evaluator.stdlname as "Evaluator ID" from students evaluatee, students evaluator, evaluations where evaluatee.stdid = evaluations.evaluateeID and evaluator.stdid = evaluations.evaluatorID order by evaluateeID; 10 STUDENTS EVALUATIONS EvaluateeEvaluator

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What was covered … Unary relationship Self-join in a query Table aliases Joining the same two tables twice in a query 11

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Resources Microsoft Faculty Connection—Faculty Resource Center Microsoft Transact-SQL Reference AdventureWorks Sample Database 12