Docsity

DATA MANAGEMENT APPLICATIONS D427 MYSQL SQL PROGRAMING INTERMEDIATE LEVEL EXPECTED QUESTIO, Exams of Relational Database Management Systems (RDBMS)

Toggle sidebar
bg1
DA
T
A
M
A
N
AGE
M
E
NT
A
PPL
ICA
T
I
ONS
D
427
MYSQL
SQL
PRO
G
R
A
M
I
N
G
I
NT
E
RM
EDIA
T
E
L
E
V
E
L
E
XP
EC
T
ED
QU
E
ST
I
ONS
W
I
T
H
V
E
R
IFIED
A
NSW
E
RS
Q
u
e
st
i
on
1.
W
hich
d
a
t
a
typ
e
w
ill
stor
e
"20220110
142212"
a
s
a
t
e
mpor
a
l
v
a
l
u
e
w
i
t
h
out
l
oss
o
f
i
n
f
orm
a
t
i
on
?
A
NSW
E
R
DA
T
E
T
I
M
E
Q
u
e
st
i
on
2.
T
he
M
ov
ie
t
a
ble
h
a
s
t
he
f
o
ll
ow
i
n
g
c
o
l
umns
:
ID
—
i
nt
ege
r
,
pr
i
m
a
ry
ke
y
,
T
i
t
le
—
v
a
r
i
a
ble
-
le
n
g
t
h
str
i
n
g
,
G
e
nr
e
—
v
a
r
i
a
ble
-
le
n
g
t
h
str
i
n
g
,
Ra
t
i
n
g
C
o
de
—
v
a
r
i
a
ble
-
le
n
g
t
h
str
i
n
g
,
Y
e
a
r
—
i
nt
ege
r
,
A
n
e
w
c
o
l
umn
must
be
a
dded
to
t
he
M
ov
ie
t
a
ble
:
C
o
l
umn
n
a
m
e
:
S
c
or
e
,
D
a
t
a
typ
e
:
deci
m
a
l
(3,1),
W
r
i
t
e
a
SQL
st
a
t
e
m
e
nt
to
a
dd
t
he
S
c
or
e
c
o
l
umn
to
t
he
M
ov
ie
t
a
ble
.
A
NSW
E
R
A
LT
E
R
T
AB
L
E
M
ov
ie
ADD
S
c
or
e
DECI
M
A
L
3,
1;
Q
u
e
st
i
on
3.
T
he
H
ors
e
t
a
ble
h
a
s
t
he
f
o
ll
ow
i
n
g
c
o
l
umns
:
ID
i
nt
ege
r
,
pr
i
m
a
ry
ke
y
,
R
egi
st
e
r
ed
Na
m
e
-
v
a
r
i
a
ble
-
le
n
g
t
h
str
i
n
g
,
B
r
eed
-
v
a
r
i
a
ble
-
le
n
g
t
h
str
i
n
g
,
H
eigh
t
-
deci
m
a
l
num
be
r
,
B
i
rt
h
D
a
t
e
-
d
a
t
e
,
W
r
i
t
e
a
S
E
L
EC
T
st
a
t
e
m
e
nt
to
s
elec
t
t
he
r
egi
st
e
r
ed
n
a
m
e
,
heigh
t
,
a
n
d
bi
rt
h
d
a
t
e
f
or
on
l
y
h
ors
e
s
t
h
a
t
h
a
v
e
a
heigh
t
be
tw
ee
n
15.0
a
n
d
16.0
(
i
n
cl
us
i
v
e
)
or
h
a
v
e
a
bi
rt
h
d
a
t
e
on
or
a
f
t
e
r
Ja
nu
a
ry
1,
2020.
A
NSW
E
R
S
elec
t
R
egi
st
e
r
ed
Na
m
e
,
H
eigh
t
,
B
i
rt
hd
a
t
e
F
rom
H
ors
e
W
HE
R
E
H
eigh
t
BE
TW
EE
N
15.0
A
N
D
16.0
OR
B
i
rt
hd
a
t
e
>=
'20200101');
1
/
32
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download DATA MANAGEMENT APPLICATIONS D427 MYSQL SQL PROGRAMING INTERMEDIATE LEVEL EXPECTED QUESTIO and more Exams Relational Database Management Systems (RDBMS) in PDF only on Docsity!

DATA MANAGEMENT APPLICATIONS D 427

MYSQL SQL PROGRAMING INTERMEDIATE LEVEL

EXPECTED QUESTIONS WITH VERIFIED ANSWERS

Question 1.

Which data type will store "2022 01  10 14  22 12" as a temporal value without loss of information?

ANSWER DATETIME

Question 2.

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 DECIMAL3, 1 ;

Question 3.

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;

Question 5.

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;

Question 9.

Which of the following is a DML command?

ANSWER INSERT, DELETE, UPDATE

Question 10.

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 );

Question 12.

Use an SQL function to calculate the sum of all the Price column values in the Products table.

ANSWER SELECT SUMPrice) FROM Products;

Question 13.

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;

Question 14.

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;

Question 20.

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 );

Question 21.

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 AZ.

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;

Question 23.

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 DECIMAL3,1;

Question 24.

List the number of customers in each country, ordered by the country with the most customers first.

ANSWER SELECT COUNTCustomerID, Country FROM Customers GROUP BY Country ORDER BY COUNTCustomerID DESC;

Which data type represents numbers with fractional values:

ANSWER DECIMAL

Question 29.

Update the City value and the Country value.

ANSWER UPDATE Customers SET City = 'Oslo', Country = 'Norway' WHERE CustomerID  32;

Question 30.

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

Question 31.

Write a sql query to drop a primary key constraint

ANSWER ALTER TABLE Persons DROP PRIMARY KEY;

Question 32.

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.CustomerIDCustomers.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 DECIMAL8, 2  CHECK Budget  0 AND Budget  999999  );

Question 34.

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;

Question 35.

Update the City column of all records in the Customers table.

ANSWER UPDATE Customers SET City = 'Oslo';

Question 36.

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 ;

Question 42.

Write the correct SQL statement to delete a table called Persons.

ANSWER DROP TABLE Persons;

Question 43.

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.CustomerIDCustomers.CustomerID;

Write a sql query to drop a FOREIGN KEY constraint

ANSWER ALTER TABLE Orders DROP FOREIGN KEY FK_PersonOrder;

Question 45.

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;

Question 46.

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;

Question 51.

Use an SQL function to calculate the average price of all products.

ANSWER SELECT AVGPrice) FROM Products;

Question 52.

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 ;

Question 53.

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;

Question 55.

Define a SELECT statement?

ANSWER SELECT column1, column2, ... FROM table_name;

Question 56.

Use the TRUNCATE statement to delete all data inside a table.

ANSWER TRUNCATE TABLE Persons;

Question 57.

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 COUNTCustomerID, Country FROM Customers GROUP BY Country;

Question 63.

A database has a view named MovieView. Write a SQL statement to delete the view named MovieView from the database.

ANSWER Drop View MovieView;

Question 64.

A database has a view named BookView. Write a SQL statement to delete the view named BookView from the database.

ANSWER DROP VIEW BookView;

Question 65.

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 );

Question 66.

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;

Question 67.

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 AZ. 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;