Free Essay

Outer Join and Scalar Queries

In:

Submitted By Mozel54
Words 414
Pages 2
Write the following queries in SQL:
Display a list of all instructors, showing their ID, name, and the number of sections that they have taught. Make sure to show the number of sections as 0 for instructors who have not taught any section. Your query should use an outer join, and should not use scalar subqueries.

By using the university schema provided by db-book.com the following queries were done on the university database. The first query uses an outer join which works similar to the join operation but it keeps the rows that don’t match between the two tables that would be lost in a join operation. There are three forms of outer join: a) Left outer join displays the results from the left table even if the condition does not find any matching record in the right table. b) Right outer join will displays the results from the right table regardless if there is matching data in the left table. c) Full outer join will retain all rows from both tables, regardless if the data matches or not.

The group by clause when used in a select statement collects data from multiple records and groups the results into one or more columns.

The below is the query using left outer join operation: select ID, name, count(sec_id) as Number_of_sections from instructor natural left outer join teaches group by ID, name;
The next query was written by using a scalar subquery without using an outer join operation. A scalar subquery is where the output of a subquery returns only one row which contains one single column/attribute. By using the select statement with count (*) this guarantees that the query will select one column/attribute and returns the value in just one row. Scalar subqueries may occur in select, where, and having clauses (Silberschatz, Korth & Sudarshan, 2010).

The below query represent scalar subquery: select ID, name,
(select count(*) from teaches B where B.id=F.id) as Number_of_sections from instructor F;

The results (see below) for both queries are the same – each query extracted twelve instructor records from the database. It counted and combined sections that the instructors had taught and display instructors who have not taught any section thus far.

ID | name | Number_of_sections | 10101 | Srinivasan | 3 | 12121 | Wu | 1 | 15151 | Mozart | 1 | 22222 | Einstein | 1 | 32343 | El Said | 1 | 33456 | Gold | 0 | 45565 | Katz | 2 | 58583 | Califieri | 0 | 76543 | Singh | 0 | 76766 | Crick | 2 | 83821 | Brandt | 3 | 98345 | Kim | 1 |

Similar Documents

Premium Essay

Data Base Concepts

...Jonathan Hindman PT2520 Chapter 4 10/18/13 Cardinality: Cardinality refers to the number of allowed instances of a relationship. In the usual cardinality of one to many, for instance, each record on the one side can have zero to any number of records on the many side. Cardinality can be more specific however. Each patron at a library can have only 20 items checked out at once. This has a cardinality of 0 to 20. Composite keys: In database design, a compound key is a key that consists of 2 or more attributes that uniquely identify an entity occurrence. Each attribute that makes up the compound key is a simple key in its own right. Crow’s feet notation: A type of notation for entity relationships in entity relation diagrams that depicts the many side of a relationship with a three-pronged end called a “crows foot.” This type of notation provides more information about the cardinality of a relationship than the arrow notation for relationships. Domain entities: Those database entities that relate directly to the business problem under consideration. Entity relation diagrams: A diagram that shows entities, their attributes, and the relationships among them. Linking entity: An entity used to resolve a many-to-many relationship into two one-to-many relationships. Logical entity: The design of a database without regard to the physical implementation of the database. Lookup entity: An entity used to store lookup values such as state names or zip codes. Maximum cardinality:...

Words: 1477 - Pages: 6

Free Essay

It- Sql Server 2012

...Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Prerequisites: Before attending this course, students must have: Working knowledge of relational databases. Basic knowledge of the Microsoft Windows operating system and its core functionality. Before attending this course, students should have: Basic understanding of virtualization technology (Classroom labs utilize virtual machines) To help you prepare for this class, review the following resources: Book: T-SQL Fundamentals for Microsoft SQL Server 2012 and SQL Azure MCTS Self Paced Training Kit 70-461 About this Course This 5-day instructor led course provides students with the technical skills required to write basic Transact-SQL queries for Microsoft SQL Server 2012. This course is the foundation for all SQL Server-related disciplines; namely, Database Administration, Database Development and Business Intelligence. This course helps people prepare for exam 70-461. All the labs for this course can be performed using the provided virtual machines. However, if you have a Microsoft Windows Azure account and the classroom virtual machines connect to the internet you may be able to connect to your Windows Azure server and database from the classroom. Many of the labs in this course are enabled for you to perform the lab while connected to your own Windows Azure database in the cloud. Your instructor should be able to provide a current list of Windows Azure enabled labs. To acquire a trial Windows Azure account click...

Words: 1666 - Pages: 7

Free Essay

Distributed Systems

...The database development process * Query and application development tools * CASE tools for database analysis and design * Tables, attributes and relationships * Primary and foreign keys * Relational integrity constraints * Manipulating data: selection, projection, join, union, intersection, difference * An integrated, active data dictionary * The query optimizer * Developing the logical data model * Mapping the data model to the relational model * Specifying integrity constraints * Defining the data in the data dictionary * Capturing entities, attributes and identifiers * Describing relationships: one-to-one, one-to-many, many-to-many * Optional and mandatory relationships * Resolving many-to-many relationships for implementation * Generating the SQL to build the database * Reverse engineering to capture the design of an existing database * SQL Programming Language Introduction 1 Days * Write SQL code based on ANSI/ISO standards to build Microsoft SQL Server or Oracle database structures * Update database content with SQL and transaction handling * Retrieve data with filter conditions and from multiple tables using various types of join * Process data with row and aggregate functions * SQL Server Introductory Courses * SQL Server Introduction 1Days * Create and administer SQL Server 2012 or 2014 databases * Write Transact-SQL queries to retrieve and manipulate data *...

Words: 1010 - Pages: 5

Premium Essay

Database - Sql

...Final Exam Review Below are the review questions for your final exam. You MUST complete this as it is a part of your final exam equaling total of 10 points. Each question must be answered with at least 3 sentences, clearly demonstrating an understanding of the question asked. This is due on the final day in class prior to the final exam. Questions 1. What is a database? Describe the 3 main types discussed in the book. * Is a collection of related data. * Doesn’t have to be electronic; example : card catalog’s in libraries ; spiral notebook ; phone or address book * Usually mean electronic database that run on computers 2. Why is the relational database model most commonly used today? What impact does it have on data integrity? * The idea came from Edgar F Codd in 1970 he worked for IBM at the time * Advantage is the tables define the relationships among themselves by mean of repeating an attribute or column from one table in another table “called Keys” * One of the problems was data redundancy; means to store the same data in more than place in the database * Codd’s theoretical design minimized redundancy * The impact is; the complexity of the design, its easy to create a hard to use database, or hard to fine the data you entered into the database. So a well-designed database = data integrity and flexibility. 3. Distinguish between data integrity and redundancy. * Data integrity = to the accuracy and quality of the...

Words: 2656 - Pages: 11

Premium Essay

Study Guide

...® OCA Oracle Database 11g: SQL Fundamentals I Exam Guide (Exam 1Z0-051) ABOUT THE AUTHORS John Watson (Oxford, UK) works for BPLC Management Consultants, teaching and consulting throughout Europe and Africa. He was with Oracle University for several years in South Africa, and before that worked for a number of companies, government departments, and NGOs in England and Europe. He is OCP qualified in both database and Application Server administration. John is the author of several books and numerous articles on technology and has 25 years of experience in IT. Roopesh Ramklass (South Africa), OCP, is an independent Oracle specialist with over 10 years of experience in a wide variety of IT environments. These include software design and development, systems analysis, courseware development, and lecturing. He has worked for Oracle Support and taught at Oracle University in South Africa for several years. Roopesh is experienced in managing and executing IT development projects, including infrastructure systems provisioning, software development, and systems integration. About the Technical Editor Bruce Swart (South Africa) works for 2Cana Solutions and has over 14 years of experience in IT. Whilst maintaining a keen interest for teaching others, he has performed several roles including developer, analyst, team leader, administrator, project manager, consultant, and lecturer. He is OCP qualified in both database and developer roles. He has taught at Oracle University...

Words: 150089 - Pages: 601

Premium Essay

Gfhjgfjdghjg

...more than one place in the same database. d. You have more than one database devoted to the same thing. 2. Which statement best describes the function of a primary key? a. It uniquely identifies each record. b. It is always a number. c. It is the first field in a table. d. It unlocks the data in a table. 3. Which statement best describes the function of a foreign key? a. A foreign key is a key that doesn't belong in the table. b. A foreign key is the primary key of one table repeated in another table in order to create a relationship between the tables. c. A foreign key is redundant data. d. A foreign key is a field telling what language the data is in. 4. What is SQL? a. It is the language relational databases use to query the database. b. It is the language relational databases use to create database objects and to manipulate and retrieve data. c. It is a language like Java or C# for creating applications. d. It is the name of Microsoft's relational database manager. 5. A database entity can best be described as _____________. a. the person responsible for the database b. the database itself c. something the database is concerned with about for which data can be stored d. something that describes a person or process 6. Which of the following best defines the term “database requirement”? a. Something a manager wants from the database b. An exception to a rule or practice c. Something a database must do to fulfill its business function d. The...

Words: 5623 - Pages: 23

Premium Essay

Sql Plus

...Introduction to Oracle9i: SQL Instructor Guide • Volume 1 40049GC11 Production 1.1 October 2001 D33993 Authors Copyright © Oracle Corporation, 2000, 2001. All rights reserved. Nancy Greenberg Priya Nathan This documentation contains proprietary information of Oracle Corporation. It is provided under a license agreement containing restrictions on use and disclosure and is also protected by copyright law. Reverse engineering of the software is prohibited. If this documentation is delivered to a U.S. Government Agency of the Department of Defense, then it is delivered with Restricted Rights and the following legend is applicable: Technical Contributors and Reviewers Josephine Turner Martin Alvarez Anna Atkinson Don Bates Marco Berbeek Andrew Brannigan Restricted Rights Legend Use, duplication or disclosure by the Government is subject to restrictions for commercial computer software and shall be deemed to be Restricted Rights software under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013, Rights in Technical Data and Computer Software (October 1988). This material or any portion of it may not be copied in any form or by any means without the express prior written permission of Oracle Corporation. Any other copying is a violation of copyright law and may result in civil and/or criminal penalties. Laszlo Czinkoczki Michael Gerlach Sharon Gray Rosita Hanoman Mozhe Jalali Sarah Jones Charbel Khouri ...

Words: 33197 - Pages: 133

Premium Essay

Paper

...Introduction to Oracle9i: SQL Instructor Guide • Volume 1 40049GC11 Production 1.1 October 2001 D33993 Authors Nancy Greenberg Priya Nathan Copyright © Oracle Corporation, 2000, 2001. All rights reserved. This documentation contains proprietary information of Oracle Corporation. It is provided under a license agreement containing restrictions on use and disclosure and is also protected by copyright law. Reverse engineering of the software is prohibited. If this documentation is delivered to a U.S. Government Agency of the Department of Defense, then it is delivered with Restricted Rights and the following legend is applicable: Restricted Rights Legend Use, duplication or disclosure by the Government is subject to restrictions for commercial computer software and shall be deemed to be Restricted Rights software under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013, Rights in Technical Data and Computer Software (October 1988). This material or any portion of it may not be copied in any form or by any means without the express prior written permission of Oracle Corporation. Any other copying is a violation of copyright law and may result in civil and/or criminal penalties. If this documentation is delivered to a U.S. Government Agency not within the Department of Defense, then it is delivered with “Restricted Rights,” as defined in FAR 52.227-14, Rights in Data-General, including Alternate III (June 1987). The information in this document...

Words: 33172 - Pages: 133

Free Essay

Abc Corporation

...Oracle TimesTen In-Memory Database SQL Reference Guide Release 7.0 B31682-03 Copyright ©1996, 2007, Oracle. All rights reserved. ALL SOFTWARE AND DOCUMENTATION (WHETHER IN HARD COPY OR ELECTRONIC FORM) ENCLOSED AND ON THE COMPACT DISC(S) ARE SUBJECT TO THE LICENSE AGREEMENT. The documentation stored on the compact disc(s) may be printed by licensee for licensee’s internal use only. Except for the foregoing, no part of this documentation (whether in hard copy or electronic form) may be reproduced or transmitted in any form by any means, electronic or mechanical, including photocopying, recording, or any information storage and retrieval system, without the prior written permission of TimesTen Inc. Oracle, JD Edwards, PeopleSoft, Retek, TimesTen, the TimesTen icon, MicroLogging and Direct Data Access are trademarks or registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. The Programs (which include both the software and documentation) contain proprietary information; they are provided under a license agreement containing restrictions on use and disclosure and are also protected by copyright, patent, and other intellectual and industrial property laws. Reverse engineering, disassembly, or decompilation of the Programs, except to the extent required to obtain interoperability with other independently created software or as specified by law, is prohibited. The information contained in this document is subject...

Words: 82065 - Pages: 329

Premium Essay

B2B Advantages and Disadvantages

...This page intentionally left blank Te n t h E d i t i o n MODERN DATABASE MANAGEMENT Editorial Director: Sally Yagan Editor in Chief: Eric Svendsen Executive Editor: Bob Horan Editorial Project Manager: Kelly Loftus Editorial Assistant: Jason Calcano Director of Marketing: Patrice Lumumba Jones Marketing Manager: Anne Fahlgren Marketing Assistant: Melinda Jensen Senior Managing Editor: Judy Leale Project Manager: Becca Richter Senior Operations Supervisor: Arnold Vila Operations Specialist: Ilene Kahn Senior Art Director: Jayne Conte Cover Designer: Suzanne Behnke Cover Art: Fotolia © vuifah Manager, Visual Research: Karen Sanatar Permissions Project Manager: Shannon Barbe Media Project Manager, Editorial: Denise Vaughn Media Project Manager, Production: Lisa Rinaldi Supplements Editor: Kelly Loftus Full-Service Project Management: PreMediaGlobal Composition: PreMediaGlobal Printer/Binder: Edwards Brothers Cover Printer: Lehigh-Phoenix Color/Hagerstown Text Font: Palatino Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on appropriate page within text. Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. Screen shots and icons reprinted with permission from the Microsoft Corporation. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. Copyright © 2011, 2009, 2007, 2005, 2002...

Words: 193467 - Pages: 774

Premium Essay

Windows Operating System

...of designing and developing databases that they can apply in their future employment. They need to create tables, enter data, and run SQL queries. This book is designed for them. Hands on Database: an Introduction to Database Design and Development focuses on the process of creating a database. It guides the student through the initial conception of the database. It covers gathering of requirements and business rules, the logical and physical design and the testing of the database. It does this through a continuous narrative that follows a student, Sharon, as she designs and constructs a database to track the tutoring program at her school. It shows some of her missteps as well as her successes. Students get hands-on experience by doing practices and developing scenarios that parallel the narrative. After completing this book students will have a good sense of what is involved in developing and creating a database. Below is a list of the book outcomes. A student who has completed this book will be able     to give a general definition of a relational database to identify a variety of ways to gather database requirements to define business rules for a database to create an Entity design for a database 2 Hands ON Database      to normalize a design up to third normal form to develop a database in a given DBMS to run SQL Queries against sample data to test requirements and business rules to define the general security context of a database and its users to document...

Words: 66591 - Pages: 267

Free Essay

Donald Miner Serves as a Solutions Architect at Emc Greenplum,

...www.it-ebooks.info MapReduce Design Patterns Donald Miner and Adam Shook www.it-ebooks.info MapReduce Design Patterns by Donald Miner and Adam Shook Copyright © 2013 Donald Miner and Adam Shook. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Andy Oram and Mike Hendrickson Production Editor: Christopher Hearse Proofreader: Dawn Carelli Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest December 2012: First Edition Revision History for the First Edition: 2012-11-20 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449327170 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. MapReduce Design Patterns, the image of Père David’s deer, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the...

Words: 63341 - Pages: 254

Premium Essay

Hands on Database

...HANDS-ON DATABASE AN INTRODUCTION TO DATABASE DESIGN AND DEVELOPMENT Steve Conger Seattle Central Community College Prentice Hall Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo Editorial Director: Sally Yagan Editor in Chief: Eric Svendsen Executive Editor: Bob Horan Product Development Manager: Ashley Santora Editorial Project Manager: Kelly Loftus Editorial Assistant: Jason Calcaño Director of Marketing: Patrice Lumumba Jones Senior Marketing Manager: Anne Fahlgren Marketing Assistant: Melinda Jensen Production Project Manager: Renata Butera Creative Art Director: Jayne Conte Cover Designer: Suzanne Behnke Cover Art: Kheng Guan Toh/Fotolia, Inc Media Editor: Denise Vaughn Media Project Manager: Lisa Rinaldi Full-Service Project Management: Chitra Sundarajan/Integra Software Services Pvt. Ltd. Printer/Binder: Edwards Brothers Cover Printer: Lehigh-Phoenix Color/Hagerstown Text Font: Palatino Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. Screen shots and icons reprinted with permission from the Microsoft Corporation. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. Copyright © 2012 Pearson Education, Inc., publishing as Prentice Hall, One Lake Street, Upper Saddle...

Words: 70521 - Pages: 283

Premium Essay

Deep'Z Studio.

...1 INTRODUCTION SQL is divided into the following  Data Definition Language (DDL)  Data Manipulation Language (DML)  Data Retrieval Language (DRL)  Transaction Control Language (TCL)  Data Control Language (DCL) DDL -- create, alter, drop, truncate, rename DML -- insert, update, delete DRL -- select TCL -- commit, rollback, savepoint DCL -- grant, revoke CREATE TABLE SYNTAX Create table (col1 datatype1, col2 datatype2 …coln datatypen); Ex: SQL> create table student (no number (2), name varchar (10), marks number (3)); INSERT This will be used to insert the records into table. We have two methods to insert.   a) By value method By address method USING VALUE METHOD Syntax: insert into (table_name) values (value1, value2, value3 …. Valuen); © Copy rights are reserved. 2 Ex: SQL> insert into student values (1, ’sudha’, 100); SQL> insert into student values (2, ’saketh’, 200); To insert a new record again you have to type entire insert command, if there are lot of records this will be difficult. This will be avoided by using address method. b) USING ADDRESS METHOD Syntax: insert into (table_name) values (&col1, &col2, &col3 …. &coln); This will prompt you for the values but for every insert you have to use forward slash. Ex: SQL> insert into student values (&no, '&name', &marks); Enter value for no: 1 Enter value for name: Jagan Enter value for marks: 300 old new ...

Words: 42387 - Pages: 170

Premium Essay

Databasse Management

...Fundamentals of Database Systems Preface....................................................................................................................................................12 Contents of This Edition.....................................................................................................................13 Guidelines for Using This Book.........................................................................................................14 Acknowledgments ..............................................................................................................................15 Contents of This Edition.........................................................................................................................17 Guidelines for Using This Book.............................................................................................................19 Acknowledgments ..................................................................................................................................21 About the Authors ..................................................................................................................................22 Part 1: Basic Concepts............................................................................................................................23 Chapter 1: Databases and Database Users..........................................................................................23 ...

Words: 229471 - Pages: 918