
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























Which data type will store "2022 01 10 14 22 12" as a temporal value without loss of information?
ANSWER DATETIME
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, A new column must be added to the Movie table: Column name: Score, Data type: decimal(3,1), Write a SQL statement to add the Score column to the Movie table.
ANSWER ALTER TABLE Movie ADD Score DECIMAL3, 1 ;
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.0 and 16.0 (inclusive) or have a birth date on or after January 1, 2020.
ANSWER Select RegisteredName, Height, Birthdate From Horse WHERE Height BETWEEN 15.0 AND 16.0 OR Birthdate >= '2020 01 01');
The database has three tables for tracking horse-riding lessons:
Horse with columns: ID primary key, RegisteredName, Breed, Height, BirthDate,
Student with columns: ID primary key, FirstName, LastName, Street, City, State, Zip, Phone, EmailAddress,
LessonSchedule with columns: 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 with the lesson date/time, horse ID, and the student's first and last names. Order the results in ascending order by lesson date/time, then by horse ID. Unassigned lesson times (student ID is NULL should not appear in the schedule. Hint: Perform a join on the Student and LessonSchedule tables, matching the student IDs.
ANSWER SELECT LS.LessonDateTime, LS.HorseID, S.FirstName, S.LastName From LessonSchedule LS Join Student S on LS.StudentID S.ID Where LS.StudentID is not Null Order BY LS.LessonDateTime ASC, LS.HorseID ASC;
Write the correct SQL statement to create a new database called testDB.
ANSWER CREATE DATABASE testDB;
City, State, Zip, Phone, EmailAddress,
LessonSchedule with columns: 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 Horse tables, matching the student IDs and horse IDs.
ANSWER SELECT LS.LessonDateTime, S.FirstName, S.Last-Name, H.RegisteredName FROM LessonSchedule LS LEFT JOIN Student S ON LS.StudentID S.ID LEFT JOIN Horse H ON LS.HorseID H.ID WHERE LS.LessonDateTime >= '2020 02 01' AND LS.LessonDateTime < '2020 02 02' ORDER BY LS.LessonDateTime ASC, H.RegisteredName ASC;
Which of the following is a DML command?
ANSWER INSERT, DELETE, UPDATE
Set the value of the City columns to 'Oslo', but only the ones where the Country column has the value "Norway".
ANSWER UPDATE Customers SET City = 'Oslo' WHERE Country = 'Norway';
Write a sql query to create a PRIMARY KEY on the "ID" column on the "Persons" table
ANSWER CREATE TABLE Persons ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, PRIMARY KEY ID );
Use an SQL function to calculate the sum of all the Price column values in the Products table.
ANSWER SELECT SUMPrice) FROM Products;
The Book table has the following columns: ID—integer, primary key, Title—variable-length string, Genre—variable-length string, Year—integer, Write a SQL statement to create a view named My-Books that contains the Title, Genre, and Year columns for all movies books. Ensure your result set returns the columns in the order indicated.
ANSWER CREATE VIEW MyBooks AS SELECT Title, Genre, Year FROM Book;
Write an SQL statement to allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns
ANSWER CREATE TABLE Orders OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int, PRIMARY KEY OrderID, CONSTRAINT FK_Person-Order FOREIGN KEY PersonID REFERENCES Persons(PersonID );
The Book table has the following columns: ID—integer, primary key, auto_increment, Title—variable- length string, Genre—variable-length string, Year—integer, The YearSales table has the following columns: Year—integer, TotalSales—bigint unsigned, Releases—integer, Write a SQL query to display both the Title and the TotalSales (if available) for all books. Ensure your result set returns the columns in the order indicated.
ANSWER SELECT Title, TotalSales FROM Book LEFT JOIN YearSales ON Book.Year YearSales.Year;
Create a FOREIGN KEY on the table Orders with OrderID as Primary KEY, and PersonID as the FOREIGN KEY.
ANSWER CREATE TABLE Orders OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int, PRIMARY KEY OrderID, FOREIGN KEY PersonID REFERENCES Persons(PersonID );
The Book table has the following columns: ID—integer, primary key, auto_increment, Title—variable- length string, Genre—variable-length string, Year—integer, Write a SQL query to display all Title values in alphabetical order AZ.
ANSWER SELECT Title FROM Book ORDER BY Title ASC;
Create a CREATE VIEW statement, followed by the view name, the column names, and the SELECT query that defines the view's data.
ANSWER CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition;
The Automobile table has the following columns: ID—integer, primary key, Make—variable-length string, Model—variable-length string, Year—integer, A new column must be added to the Automobile table: Column name: SafetyRating, Data type: decimal(3,1), Write a SQL statement to add the SafetyRating column to the Automobile table.
ANSWER ALTER TABLE Automobile ADD SafetyRating DECIMAL3,1;
List the number of customers in each country, ordered by the country with the most customers first.
ANSWER SELECT COUNTCustomerID, Country FROM Customers GROUP BY Country ORDER BY COUNTCustomerID DESC;
Which data type represents numbers with fractional values:
ANSWER DECIMAL
Update the City value and the Country value.
ANSWER UPDATE Customers SET City = 'Oslo', Country = 'Norway' WHERE CustomerID 32;
The Book table has the following columns: genre - varchar(20), pages - integer, author_id - char(3), isbn_number - varchar(20), Which column should be designated at the primary key for the Book table?
ANSWER isbn_number
Write a sql query to drop a primary key constraint
ANSWER ALTER TABLE Persons DROP PRIMARY KEY;
JOIN clause to select all records from the two tables where there is a match in both tables.
ANSWER SELECT * FROM Orders INNER JOIN Customers ON Orders.CustomerIDCustomers.CustomerID;
Create a Movie table with the following columns: ID positive integer with maximum value of 50,000, 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,999 dollars, with 2 digits for cents,
ANSWER CREATE TABLE Movie ( ID SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, Title VARCHAR 50 , Rating CHAR 4 , ReleaseDate DATE, Budget DECIMAL8, 2 CHECK Budget 0 AND Budget 999999 );
The Book table has the following columns: ID—integer, primary key, Title—variable-length string, Genre—variable-length string, Year—integer, Write a SQL statement to modify the Book table to make the ID column the primary key.
ANSWER ALTER TABLE Book ADD PRIMARY KEY ID;
Update the City column of all records in the Customers table.
ANSWER UPDATE Customers SET City = 'Oslo';
Once a view is created, create a query to view it.
ANSWER SELECT * FROM Named-View;
ID integer, primary key, RegisteredName - variable-length string,
Student with columns: ID integer, primary key, FirstName - variable-length string, LastName - variable-length string,
Create the LessonSchedule table with columns: HorseID integer with range 0 to 65 thousand, not NULL, partial primary key, foreign key references Horse(ID), StudentID integer with range 0 to 65 thousand, foreign key references Student(ID), LessonDateTime - date/time, not NULL, partial primary key, 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.
ANSWER CREATE TABLE LessonSchedule ( HorseID INT NOT NULL, StudentID INT, 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 ;
Write the correct SQL statement to delete a table called Persons.
ANSWER DROP TABLE Persons;
JOIN clause to join the two tables Orders and Customers, using the CustomerID field in both tables as the relationship between the two tables.
ANSWER SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerIDCustomers.CustomerID;
Write a sql query to drop a FOREIGN KEY constraint
ANSWER ALTER TABLE Orders DROP FOREIGN KEY FK_PersonOrder;
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.
ANSWER SELECT E.FirstName AS Employee, M.FirstName AS Manager FROM Employee E JOIN Employee M ON E.ManagerID M.ID ORDER BY E.FirstName;
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 update the Year value to be 2022 for all movies with a Year value of 2020.
ANSWER UPDATE MOVIE SET Year 2022 WHERE Year 2020;
In Employee table, create query to update salary of all employees who make less than 70 k, add 1000 to their salary.
ANSWER UPDATE your_table_name SET salary = salary 1000 WHERE salary 70000;
Use an SQL function to calculate the average price of all products.
ANSWER SELECT AVGPrice) FROM Products;
The Book table has the following columns: ID—integer, primary key, auto_increment Title—variable- length string, Genre—variable-length string, Year—integer, The following data needs to be added to the Book table: Title Genre Year, The Joy Luck Club, Fiction, 1989, Write a SQL statement to insert the indicated data into the Book table.
ANSWER INSERT INTO Book Title, Genre, Year) VALUES ('The Joy Luck Club', 'Fiction', 1989 ;
Use the correct function to return the number of records that have the Price value set to 18.
ANSWER SELECT COUNT* FROM Products WHERE Price 18;
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.
ANSWER SELECT Year, COUNT* AS TotalMovies FROM Movie GROUP BY Year;
Define a SELECT statement?
ANSWER SELECT column1, column2, ... FROM table_name;
Use the TRUNCATE statement to delete all data inside a table.
ANSWER TRUNCATE TABLE Persons;
Insert a new record in the Customers table.
ANSWER INSERT INTO Customers CustomerName, Contact-Name, Address, City, PostalCode, Country) VALUES 'Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
List the number of customers in each country.
ANSWER SELECT COUNTCustomerID, Country FROM Customers GROUP BY Country;
A database has a view named MovieView. Write a SQL statement to delete the view named MovieView from the database.
ANSWER Drop View MovieView;
A database has a view named BookView. Write a SQL statement to delete the view named BookView from the database.
ANSWER DROP VIEW BookView;
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, LastName - variable-length string with max 30 chars, not NULL, Street - variable-length string with max 50 chars, not NULL, City - variable-length string with max 20 chars, not NULL, State - fixed-length string of 2 chars, not NULL, default "TX", Zip - integer with range 0 to 16 million, not NULL, Phone - fixed-length string of 10 chars, not NULL, Email - variable-length string with max 30 chars, must be unique,
ANSWER CREATE TABLE Student( ID SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, FirstName VARCHAR 20 NOT NULL, LastName VARCHAR 30 NOT NULL, Street VARCHAR 50 NOT NULL, City VARCHAR 20 NOT NULL, State CHAR 2 NOT NULL DEFAULT 'TX',
Zip MEDIUMINT UNSIGNED CHECK Zip 0 AND Zip 16000000 NOT NULL, Phone CHAR 10 NOT NULL, Email VARCHAR 30 UNIQUE );
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.
ANSWER ALTER TABLE Movie ADD PRIMARY KEY ID;
The Book table has the following columns: ID—integer, primary key, auto_increment, Title—variable- length string, Genre—variable-length string, Year—integer, Write a SQL query to output the unique Genre values and the number of books with each genre value from the Book table as GenreCount. Sort the results by the Genre in alphabetical order AZ. Ensure your result set returns the columns in the order indicated.
ANSWER SELECT Genre, COUNT* AS GenreCount FROM Book GROUP BY Genre ORDER BY Genre ASC;