Question

write an SQL statement to list all the people in the Owner table, along with their...

write an SQL statement to list all the people in the Owner table, along with their pets' names if any. That is, for each pet an owner has, there will be a record containing the owner and the pet's name. For any owner contained in the Owner table, the output should always contain the owner's record even if no pets are associated with this owner. Hint: outer join/s may be needed.

-- DROP TABLE PetAndOwner, Pet, PetType, Owner;

CREATE TABLE PetType (
  petTypeId  VARCHAR(10) PRIMARY KEY,
  animalType VARCHAR(20),
  breed      VARCHAR(20)
);

CREATE TABLE Owner (
  ownerId    VARCHAR(10) PRIMARY KEY,
  firstName  VARCHAR(20),
  lastName   VARCHAR(20) NOT NULL,
  homePhoneNumber VARCHAR(20),
  streetAddress   VARCHAR(80),
  suburb          VARCHAR(20),
  postcode        VARCHAR(10)
);

CREATE TABLE Pet (
  petId     VARCHAR(10) PRIMARY KEY,
  petName   VARCHAR(20),
  sex       CHAR(1)
            CHECK (sex IN ('M', 'F')),
  petTypeId VARCHAR(10)
            FOREIGN KEY REFERENCES PetType
);

CREATE TABLE PetAndOwner (
  ownerId    VARCHAR(10),
  petId      VARCHAR(10),
  PRIMARY KEY (ownerId, petId),
  FOREIGN KEY (ownerId) REFERENCES Owner,
  FOREIGN KEY (petId) REFERENCES Pet
);


INSERT INTO PetType VALUES ('001', 'dog', 'Bulldog');
INSERT INTO PetType VALUES ('002', 'dog', 'Lhasa Apso');
INSERT INTO PetType VALUES ('003', 'dog', 'Maltese');
INSERT INTO PetType VALUES ('004', 'cat', 'Persian');
INSERT INTO PetType VALUES ('005', 'cat', 'Ragdoll');

INSERT INTO Owner VALUES ('001', 'David', 'Smith', '12345678', '100 Victoria Road', 'Rydalmere', '2116');
INSERT INTO Owner VALUES ('002', 'Louise', 'Brown', '87654321', '1 James Ruse Road', 'Rydalmere', '2116');
INSERT INTO Owner VALUES ('003', 'Robert', 'Brown', '11223344', '2 Wentworth Street', 'Parramatta', '2150');
INSERT INTO Owner VALUES ('004', 'Avatar', 'Phantom', '',  '1 Pandora', 'Na''vi Land', '0000');

INSERT INTO Pet VALUES ('001', 'Mickey Mouse', 'M', '001');
INSERT INTO Pet VALUES ('002', 'Bugs Bunny', 'M', '001');
INSERT INTO Pet VALUES ('003', 'Betty Boop', 'F', '002');
INSERT INTO Pet VALUES ('004', 'Droopy', 'M', '003');
INSERT INTO Pet VALUES ('005', 'Penelope', 'F', '004');
INSERT INTO Pet VALUES ('006', 'Jerry', 'F', '005');

INSERT INTO PetAndOwner VALUES ('001', '001');
INSERT INTO PetAndOwner VALUES ('001', '004');
INSERT INTO PetAndOwner VALUES ('002', '001');
INSERT INTO PetAndOwner VALUES ('002', '005');
INSERT INTO PetAndOwner VALUES ('003', '002');
INSERT INTO PetAndOwner VALUES ('002', '003');
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SQL Query :

select Owner.ownerId,firstName,lastName,petName
from Owner left join PetAndOwner on
Owner.ownerId=PetAndOwner.ownerId left join Pet on
PetAndOwner.petId=pet.petId;

Screen in SQL Server :

Explanation :

  • Above sql query will join three tables Owner , PetAndOwner and Pet based on common columns
  • and will return the result.
Add a comment
Know the answer?
Add Answer to:
write an SQL statement to list all the people in the Owner table, along with their...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write SQL CREATE Table statement to create the following table with Owner ID as a...

    1. Write SQL CREATE Table statement to create the following table with Owner ID as a surrogate key. Owner ID is a surrogate key (starts at 1 and increments by 1) hint: IDENTITY (1, 1) is syntax for surrogate key. PET_OWNER (OwnerID, OwnerLastName, OwnerFirstName, OwnerPhone, OwnerEmail) You can try inserting some data into your tables too! 2. Write SQL CREATE Table statement to create the following table with Pet ID as a surrogate key. Pet ID is a surrogate key...

  • The following is the relation notation of the Veterinary Office List. VETERINARY_OFFICE (PetID, PetName, PetType, PetBreed,...

    The following is the relation notation of the Veterinary Office List. VETERINARY_OFFICE (PetID, PetName, PetType, PetBreed, OwnerID, OwnerLastName, OwnerFirstName, ServiceDescription, ServiceDate, ServiceCharge) The functional dependencies are given below: PetID -> PetName, PetType, PetBreed, OwnerID, OwnerLastName, OwnerFirstName OwnerID -> OwnerLastName, OwnerFirstName ServiceDescription -> ServiceCharge PetID, ServiceDate -> ServicedDescription, ServiceCharge Assumption: A pet belongs to only one owner, while an owner may have more than one pet. A pet receives at most one treatment on any given date. Use the functional dependencies...

  • Still working on SQL, but now trying to run some commands to pull data from the...

    Still working on SQL, but now trying to run some commands to pull data from the databases. The last 3 statements Im getting errors but I am not sure how to fix them. What do I need to do differently to those statements to make it work? create table Employee( EmpID char(10), Name varchar2(50), Salery char(10), Address varchar2(50), HireDate date, Phone char(10), Primary key(EmpID)) ; create table Inventory( ProductID char(10), ProductName varchar2(50), UnitPrice char(10), CurrentInventory char(10), MonthlySales char(10), PrecentOfPrice char(10),...

  • Sample data for these tables are shown in Figures 3-26 and 3-27. For each SQL statement...

    Sample data for these tables are shown in Figures 3-26 and 3-27. For each SQL statement you write, show the results based on these data. If possible, run the statements you write for the questions that follow in an actual DBMS, as appropriate, to obtain results. Use data types that are consistent with the DBMS you are using. If you are not using an actual DBMS, consistently represent data types by using either the MySQL, Microsoft SQL Server, or Oracle...

  • Use an INSERT INTO statement with a subquery containing a SQL string function to create user...

    Use an INSERT INTO statement with a subquery containing a SQL string function to create user names for the volunteers based on their legal names and insert them into the table. +----------------------+ | Tables_in_volunteers | +----------------------+ | address              | | email                | | funds                | | hours                | | person               | | phone                | | users                | +----------------------+ users legal names are in the address table user table is : CREATE TABLE USERS(volunteer_id INT NOT NULL, username VARCHAR(50), PRIMARY KEY...

  • MICROSOFT SQL SERVER - Create the following views 1.List the employee assigned to the most projects...

    MICROSOFT SQL SERVER - Create the following views 1.List the employee assigned to the most projects 2.List the project with the most hours SCRIPT TO BUILD DATABASE create table department ( dept_ID int not null , dept_name char(50) NOT NULL, manager_ID int not null, manager_start_date date not null, constraint Dept_PK primary key (dept_ID), constraint D_Name_AK unique (dept_name) ); insert into department values(1,'abc',1,'2019-01-08'); insert into department values(2,'abc2',2,'2019-01-08'); insert into department values(3,'abc3',2,'2019-01-08'); insert into department values(4,'abc4',2,'2019-01-08'); /*project table done*/ create table project...

  • 2. The following is the relation notation of a table for a veterinary office. [70 pts....

    2. The following is the relation notation of a table for a veterinary office. [70 pts. total] VETERINARY_OFFICE (PetID, PetName, PetType, PetBreed, OwnerID, OwnerLastName, OwnerFirstName, ServiceDescription, ServiceDate, ServiceCharge) The functional dependencies are given below: PetID -> PetName, PetType, PetBreed, OwnerID, OwnerLastName, OwnerFirstName OwnerID -> OwnerLastName, OwnerFirstName ServiceDescription -> ServiceCharge PetID, ServiceDate > ServiceDescription, ServiceCharge Assumptions: 1)A pet belongs to only one owner, 2) an owner may have more than one pet, 3A pet receives at most one treatment on any...

  • Use the SQL statements provided to create your tables. Write one SQL statement for each of...

    Use the SQL statements provided to create your tables. Write one SQL statement for each of the following problems. You can only use the conditions listed in the tasks. E.g., in task 1, you cannot manually look up pid of Information Systems undergraduate program. Here is the given code: drop table textbook_schedule cascade constraints; drop table textbook_author cascade constraints; drop table schedule cascade constraints; drop table course cascade constraints; drop table textbook cascade constraints; drop table author cascade constraints; drop...

  • /* I am executing following SQL statement for the below code but I am getting error....

    /* I am executing following SQL statement for the below code but I am getting error. Pls, upload screenshot after running it. SQL Statement: SELECT OfferNo, CourseNo FROM Offering WHERE OfferTerm = 'Sum' AND OfferYear = 2012 AND FacSSN IS NULL; */ CREATE TABLE STUDENT( StdSSN INT NOT NULL PRIMARY KEY, StdFName VARCHAR2(50), StdLName VARCHAR2(50), StdCity VARCHAR2(50), StdState VARCHAR2(2), StdZip VARCHAR2(10), StdMajor VARCHAR2(15), StdYear VARCHAR2(20) ); CREATE TABLE FACULTY( FacSSN INTEGER NOT NULL PRIMARY KEY, FacFName VARCHAR(50), FacLName VARCHAR(50), FacCity...

  • Using the code below in Oracle SQL Live I keep getting an error message for the...

    Using the code below in Oracle SQL Live I keep getting an error message for the last 4 INSERTS. The error message is: ORA-00932: inconsistent datatypes: expected DATE got NUMBER. How do I correct that error? CREATE TABLE BASE ( BASENUM CHARACTER(3) NOT NULL, BASECITY varchar(20), BASESTATE CHARACTER(2), BASEPHON varchar(10), BASEMGR varchar(10), PRIMARY KEY (BASENUM) ); CREATE TABLE TYPE ( TYPENUM CHARACTER(1) NOT NULL, TYPEDESC varchar(30), PRIMARY KEY (TYPENUM) ); CREATE TABLE TRUCK ( TNUM CHARACTER(4) NOT NULL, BASENUM CHARACTER(3),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT