Docsity

WGU D427 – CODE (Shows Code): Questions & Answers, Exams of Database Management Systems (DBMS)

Toggle sidebar
bg1
WGU D427 – CODE (Shows Code): Questions
& Answers
1.47 Write the SQL to create a Product table with the following
columns:
ID - Integer
Name - Variable-length string with maximum 40 characters
ProductType - Variable-length string with maximum 3
characters
OriginDate - Year, month, and day
Weight - Decimal number with six significant digits and one digit
after the - decimal point
Place your CREATE TABLE statement before the INSERT and
SELECT statements. Run your solution and verify the result table
contains the three inserted rows. Correct Ans - CREATE
TABLE Product (
ID INT,
Name VARCHAR(40),
ProductType VARCHAR(3),
OriginDate DATE,
Weight DECIMAL(6,1)
);
1.54 Write the SQL to create a Product table with the following
columns:
ID - Integer with range 0 to 65,535
Name - Variable-length string with maximum 40 characters
ProductType - Fixed-length string with 3 characters
OriginDateTime - Year, month, day, and time
Weight - Decimal number with variable precision and 4 bytes of
storage
Place your CREATE TABLE statement before the INSERT and
SELECT statements. Run your solution and verify the result table
contains the three inserted rows. Correct Ans - CREATE
TABLE Product (
ID SMALLINT UNSIGNED,
Name VARCHAR(40),
ProductType CHAR(3),
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download WGU D427 – CODE (Shows Code): Questions & Answers and more Exams Database Management Systems (DBMS) in PDF only on Docsity!

WGU D427 – CODE (Shows Code): Questions

& Answers

1.47 Write the SQL to create a Product table with the following columns: ID - Integer Name - Variable-length string with maximum 40 characters ProductType - Variable-length string with maximum 3 characters OriginDate - Year, month, and day Weight - Decimal number with six significant digits and one digit after the - decimal point Place your CREATE TABLE statement before the INSERT and SELECT statements. Run your solution and verify the result table contains the three inserted rows. Correct Ans - CREATE TABLE Product ( ID INT, Name VARCHAR(40), ProductType VARCHAR(3), OriginDate DATE, Weight DECIMAL(6,1) ); 1.54 Write the SQL to create a Product table with the following columns: ID - Integer with range 0 to 65, Name - Variable-length string with maximum 40 characters ProductType - Fixed-length string with 3 characters OriginDateTime - Year, month, day, and time Weight - Decimal number with variable precision and 4 bytes of storage Place your CREATE TABLE statement before the INSERT and SELECT statements. Run your solution and verify the result table contains the three inserted rows. Correct Ans - CREATE TABLE Product ( ID SMALLINT UNSIGNED, Name VARCHAR(40), ProductType CHAR(3),

OriginDateTime DATETIME, Weight FLOAT ); 1.6. The given SQL creates a Movie table and inserts some movies. The SELECT statement selects all movies released before January 1, 2000. Modify the SELECT statement to select the title and release date of PG-13 movies that are released after January 1, 2008. Run your solution and verify the result table shows just the titles and release dates for The Dark Knight and Crazy Rich Asians. Correct Ans - SELECT Title, ReleaseDate FROM Movie WHERE Rating = 'PG-13' AND ReleaseDate > '2008-01-01';

Modify the SELECT statement to only select songs that have a NULL Title value. Then run your solution, and verify the new query returns only songs by Taylor Swift and Nirvana. Correct Ans - SELECT * FROM Song WHERE Title IS NULL; 1.7. Modify the SELECT statement to select only songs that have a NULL Title or NULL Artist. Correct Ans - SELECT * FROM Song WHERE Title IS NULL OR Artist IS NULL; or SELECT * FROM Song WHERE Title = NULL OR Artist = NULL; 1.8. Write three UPDATE statements to make the following changes: Change the title from 'One' to 'With Or Without You'. Change the artist from 'The Righteous Brothers' to 'Aretha Franklin'. Change the release years of all songs after 1990 to 2021. Correct Ans - UPDATE Song SET Title = 'With Or Without You'

AlbumID INT, PRIMARY KEY (ID), FOREIGN KEY (AlbumID) REFERENCES Album(ID) ); 2.1. Modify the SELECT statement to select movies with the word 'star' somewhere in the title. Run your solution and verify the result table shows just the movies Rogue One: A Star Wars Story, Star Trek and Stargate. Correct Ans - SELECT * FROM Movie WHERE Title LIKE '%star%'; 2.2. Using the YEAR() and MONTH() functions, modify the SELECT statement to select movies that are released after 2017 or in November. Correct Ans - 223SELECT * FROM Movie Where YEAR(ReleaseDate) > 2017 OR MONTH (ReleaseDate) = 11; 2.3.6 The given SQL creates a Song table and inserts some songs. The SELECT statement selects the genre and row count for each genre group. Add a new column to the SELECT statement that uses MAX() to find the most recent release year for each genre. Then add a HAVING clause that selects only genre groups that have more than one row count. Run your solution and verify country pop, R&B, and grunge genres, row counts, and most recent release years appear in the result table. Correct Ans - CREATE TABLE Song ( ID INT, Title VARCHAR(60), Artist VARCHAR(60), ReleaseYear INT, Genre VARCHAR(20), PRIMARY KEY (ID) ); INSERT INTO Song VALUES

(100, 'Hey Jude', 'Beatles', 1968, 'pop rock'), (200, 'You Belong With Me', 'Taylor Swift', 2008, 'country pop'), (300, 'You're Still the One', 'Shania Twain', 1998, 'country pop'), (400, 'Need You Now', 'Lady Antebellum', 2011, 'country pop'), (500, 'You've Lost That Lovin' Feeling', 'The Righteous Brothers', 1964, 'R&B'), (600, 'That's The Way Love Goes', 'Janet Jackson', 1993, 'R&B'), (700, 'Smells Like Teen Spirit', 'Nirvana', 1991, 'grunge'), (800, 'Even Flow', 'Pearl Jam', 1992, 'grunge'), (900, 'Black Hole Sun', 'Soundgarden', 1994, 'grunge'); -- Modify the SELECT statement SELECT Genre, COUNT(), MAX(ReleaseYear) FROM Song GROUP BY Genre HAVING COUNT () > 1; 2.4. The SQL below creates Genre and Song tables, inserts some genres and songs, and performs an inner join. Run the SQL. Verify the result table does not include songs with NULL genre or genres that are not associated with songs. Make the following changes: In the CREATE TABLE statement for Song, rename GenreCode to Code. Modify the SELECT statement to work with the new name. Run the SQL and verify the result table is unchanged. Modify the SELECT statement to perform a left join. Run the SQL and verify the result table includes songs with NULL genre. Modify the SELECT statement to perform a right join. Run the SQL and verify the result table includes genres that are not associated with any songs. Combine the left and right joins into one statement that performs a full join. Run the SQL and verify the result table includes all songs and genres. Hints: In steps 2 and 3, use keywords LEFT and RIGHT. In step 4, use key Correct Ans - SELECT * FROM Song INNER JOIN Genre ON GenreCode = Code;

2.7.2 Correct Ans - 2.7.4 Correct Ans - 2.7.6 Correct Ans - 2.7.8 Correct Ans -

The Movie table has the following columns: ID - integer, primary key Title - variable-length string Genre - variable-length string RatingCode - variable-length string Year - integer Write a SELECT statement to select the year and the total number of movies for that year. Hint: Use the COUNT() function and GROUP BY clause. Correct Ans - SELECT YEAR, COUNT(*) AS TNMYear FROM MOVIE GROUP BY YEAR;

The Movie table has the following columns: ID - integer, primary key Title - variable-length string Genre - variable-length string RatingCode - variable-length string Year - integer The Rating table has the following columns: Code - variable-length string, primary key Description - variable-length string Write a SELECT statement to select the Title, Year, and rating Description. Display all movies, whether or not a RatingCode is available. Hint: Perform a LEFT JOIN on the Movie and Rating tables, matching the RatingCode and Code columns. Correct Ans - SELECT mov.TITLE, mov.YEAR, rat.DESCRIPTION FROM Movie AS mov

LEFT JOIN Rating AS rat ON mov.RatingCode=rat.Code

The Employee table has the following columns: ID - integer, primary key FirstName - variable-length string LastName - variable-length string ManagerID - integer Write a SELECT statement to show a list of all employees' first names and their managers' first names. List only employees that have a manager. Order the results by Employee first name. Use aliases to give the result columns distinctly different names, like "Employee" and "Manager". Hint: Join the Employee table to itself using INNER JOIN. Correct Ans - SELECT L.FirstName As Employee, R.FirstName AS Manager FROM Employee AS L INNER JOIN Employee AS R ON L.ManagerID=R.ID WHERE L.ManagerID IS NOT NULL ORDER BY L.FirstName ; 2.12 Select lesson schedule with inner join The database has three tables for tracking horse-riding lessons:

  1. Horse with columns: ID - primary key RegisteredName Breed Height BirthDate
  2. Student with columns: ID - primary key FirstName LastName Street City State

HorseID - partial primary key, foreign key references Horse(ID) StudentID - foreign key references Student(ID) LessonDateTime - partial primary key Write a SELECT statement to create a lesson schedule for Feb 1, 2020 with the lesson date/time, student's first and last names, and the horse's registered name. Order the results in ascending order by lesson date/time, then by the horse's registered name. Make sure unassigned lesson times (student ID is NULL) appear in the results. Hint: Perform a join on the LessonSchedule, Student, and Correct Ans - SELECT LesSch.LessonDateTime,Student.FirstName,Student.LastName, Horse.RegisteredName FROM LessonSchedule AS LesSch LEFT JOIN Student ON LesSch.StudentID = Student.ID LEFT JOIN Horse ON LesSch.HorseID = Horse.ID WHERE LessonDateTime >= '2020-02-01 00:00:00' AND LesSch.LessonDateTime < '2020-02-01 23:59:59' ORDER BY LessonDateTime, Horse.RegisteredName; 2.14 Select tall horses with subquery The Horse table has the following columns: ID - integer, primary key RegisteredName - variable-length string Breed - variable-length string Height - decimal number BirthDate - date Write a SELECT statement to select the registered name and height for only horses that have an above average height. Order the results by height (ascending). Hint: Use a subquery to find the average height. Correct Ans - SELECT RegisteredName, Height FROM Horse WHERE Height > (SELECT AVG(Height) FROM Horse) ORDER BY HEIGHT;

7.1 Alter Movie table The Movie table has the following columns: ID - positive integer Title - variable-length string Genre - variable-length string RatingCode - variable-length string Year - integer Write ALTER statements to make the following modifications to the Movie table: Add a Producer column with VARCHAR data type (max 50 chars). Remove the Genre column. Change the Year column's name to ReleaseYear, and change the data type to SMALLINT. Note: Your SQL code does not display any results in Develop mode. Use Submit mode to test your code. Correct Ans - ALTER TABLE Movie ADD Producer VARCHAR(50), DROP COLUMN Genre, CHANGE COLUMN Year ReleaseYear SMALLINT; 7.2 Insert rows into Horse table The Horse table has the following columns: ID - integer, auto increment, primary key RegisteredName - variable-length string Breed - variable-length string, must be one of the following: Egyptian Arab, Holsteiner, Quarter Horse, Paint, Saddlebred Height - decimal number, must be between 10.0 and 20. BirthDate - date, must be on or after Jan 1, 2015 Insert the following data into the Horse table: RegisteredName Breed Height BirthDate Babe Quarter Horse 15.3 2015-02- Independence Holsteiner 16.0 2017-03- Ellie Saddlebred 15.0 2016-12- NULL Egyptian Arab 14.9 2019-10- Notes:

Change the registered name to Lady Luck and birth date to May 1, 2015 for horse with ID 4. Change every horse breed to NULL for horses born on or after December 22, 2016. Note: Your SQL code does not display any results in Develop mode. Use Submit mode to test your code. Correct Ans - UPDATE Horse SET RegisteredName ='Lady Luck',BirthDate ='2015-05-01' WHERE ID=4; UPDATE Horse SET Height=15.6 WHERE ID=2; UPDATE Horse SET Breed = Null WHERE BirthDate >='2016- 12-22'; 7.4 Delete Rows From Horse Table The Horse table has the following columns: ID - integer, auto increment, primary key RegisteredName - variable-length string Breed - variable-length string Height - decimal number BirthDate - date Delete the following rows: Horse with ID 5. All horses with breed Holsteiner or Paint. All horses born before March 13, 2013. Note: Your SQL code does not display any results in Develop mode. Use Submit mode to test your code. Correct Ans - DELETE FROM Horse WHERE ID=5; DELETE FROM Horse WHERE Breed='Holsteiner'or Breed='Paint'; DELETE FROM Horse WHERE BirthDate < '2013-03-13'; 7.5 Select horses with logical operators The Horse table has the following columns: ID - integer, primary key RegisteredName - variable-length string Breed - variable-length string

Height - decimal number BirthDate - date Write a SELECT statement to select the registered name, height, and birth date for only horses that have a height between 15. and 16.0 (inclusive) or have a birth date on or after January 1,

  1. Correct Ans - SELECT RegisteredName, Height, BirthDate FROM HORSE WHERE (HEIGHT BETWEEN 15.0 and 16.0) OR (BirthDate >= '2020-01-01'); 7.6 Create Movie Table Create a Movie table with the following columns: ID - positive integer with maximum value of 50, Title - variable-length string with up to 50 characters Rating - fixed-length string with 4 characters ReleaseDate - date Budget - decimal value representing a cost of up to 999, dollars, with 2 digits for cents Note: Your SQL code does not display any results in Develop mode. Use Submit mode to test your code. Correct Ans - CREATE Table MOVIE (ID INT, Title VARCHAR(50), Rating VARCHAR(4), ReleaseDate DATE, Budget DECIMAL (8,2) ); 7.7 Create Student table with constraints Create a Student table with the following column names, data types, and constraints: ID - integer with range 0 to 65 thousand, auto increment, primary key FirstName - variable-length string with max 20 chars, not NULL

in Develop mode. Use Submit mode to test your code. Correct Ans - CREATE TABLE Horse ( ID SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, RegisteredName VARCHAR(15) NOT NULL, Breed VARCHAR(20) CHECK (Breed IN ('Egyptian Arab','Holsteiner','Quarter Horse','Paint','Saddlebred')), Height DECIMAL(3,1) CHECK (Height >= 10.0 <= 20.0), BirthDate DATE CHECK (BirthDate >= '2015-01-01') ); 7.9 Create LessonSchedule table with FK constraints The database contains a Horse table, with columns: ID - integer, primary key RegisteredName - variable-length string The database contains a Student table, with columns: ID - integer, primary key FirstName - variable-length string LastName - variable-length string Create a third table, named LessonSchedule, with columns: HorseID - integer with range 0 to 65 thousand, not NULL, foreign key references Horse(ID) StudentID - integer with range 0 to 65 thousand, foreign key references Student(ID) LessonDateTime - date/time, not NULL Primary key is (HorseID, LessonDateTime) If a row is deleted from Horse, the rows with the same horse ID should be deleted from LessonSchedule automatically. If a row is deleted from Student, the same student IDs should be set to NULL in LessonSchedule automatically. Notes: Table and column names are case sensitive in the auto- grader. Your S Correct Ans - CREATE TABLE Horse ( ID INT NOT NULL AUTO_INCREMENT, RegisteredName VARCHAR(15), PRIMARY KEY (ID) ); CREATE TABLE Student ( ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, LastName VARCHAR(20),

FirstName VARCHAR(30), PRIMARY KEY (ID) ); CREATE TABLE LessonSchedule ( HorseID INT NOT NULL, StudentID SMALLINT UNSIGNED, LessonDateTime DATETIME NOT NULL, PRIMARY KEY (HorseID, LessonDateTime), FOREIGN KEY (HorseID) REFERENCES Horse(ID) ON DELETE CASCADE, FOREIGN KEY (StudentID) REFERENCES Student(ID) ON DELETE SET NULL ); 7.10 Correct Ans -

The Member table will have the following columns: ID—positive integer FirstName—variable-length string with up to 100 characters MiddleInitial—fixed-length string with 1 character LastName—variable-length string with up to 100 characters DateOfBirth—date AnnualPledge—positive decimal value representing a cost of up to $999,999, with 2 digits for cents Write a SQL statement to create the Member table. Do not add any additional constraints to any column beyond what is stated. Correct Ans - Create Table Member ( ID INT UNSIGNED, FirstName VARCHAR(100), MiddleInitial CHAR(1), LastName VARCHAR(100), DateOfBirth DATE, AnnualPledge DECIMAL(8,2) UNSIGNED );

The Rating table has the following columns:

Ensure your result set returns the columns in the order indicated. Correct Ans - CREATE VIEW MyMovies AS SELECT Title, Genre, Year FROM Movie;

A database has a view named MovieView. Write a SQL statement to delete the view named MovieView from the database. Correct Ans - DROP VIEW Movieview;

The Movie table has the following columns: ID—integer Title—variable-length string Genre—variable-length string RatingCode—variable-length string Year—integer Write a SQL statement to modify the Movie table to make the ID column the primary key. Correct Ans - ALTER TABLE Movie ADD PRIMARY KEY (ID);

The Movie table has the following columns: ID—integer, primary key Title—variable-length string Genre—variable-length string RatingCode—variable-length string Year—integer The YearStats table has the following columns: Year—integer TotalGross—bigint unsigned Releases—integer Write a SQL statement to designate the Year column in the Movie table as a foreign key to the Year column in the YearStats table. Correct Ans - ALTER TABLE Movie ADD CONSTRAINT Year FOREIGN KEY(Year) REFERENCES YearStats(Year);

The Movie table has the following columns: ID—integer, primary key Title—variable-length string Genre—variable-length string RatingCode—variable-length string Year—integer Write a SQL statement to create an index named idx_year on the Year column of the Movie table. Correct Ans - CREATE INDEX idx_year ON Movie(Year);

The Movie table has the following columns: ID—integer, primary key, auto-increment Title—variable-length string Genre—variable-length string RatingCode—variable-length string Year—integer The following data needs to be added to the Movie table: Title Genre RatingCode Year Pride and Prejudice Romance G 2005 Correct Ans - INSERT INTO Movie (Title,Genre,RatingCode,Year) VALUE ('Pride and Prejudice','Romance','G','2005');

The Movie table has the following columns: ID—integer, primary key Title—variable-length string Genre—variable-length string RatingCode—variable-length string Year—integer Write a SQL statement to delete the row with the ID value of 3 from the Movie table. Correct Ans - -- Your SQL goes here DELETE FROM Movie WHERE ID=3;

The Movie table has the following columns: