0% found this document useful (0 votes)
11 views

SQL Quiz

The document discusses various SQL concepts like creating databases, joins, subqueries, aggregate functions, updating and deleting rows from tables, views, calculated fields, and wildcard characters. It provides examples and brief explanations of each concept.

Uploaded by

Andy Nagel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Quiz

The document discusses various SQL concepts like creating databases, joins, subqueries, aggregate functions, updating and deleting rows from tables, views, calculated fields, and wildcard characters. It provides examples and brief explanations of each concept.

Uploaded by

Andy Nagel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a SQL Script to Create a Database called QUIZ, Size 5mb on C:\CCTB111, Maxsize
7mb and Log File 4mb. Should the Log and Data Files be on the same drive? Why?

CREATE DATABASE MYCCTB111


ON (NAME = MyDatabase,
FILENAME = 'C:\CCTB111\MYCCTB111.MDF',
SIZE = 5MB,
MAXSIZE = 7MB)

LOG ON
(NAME = MyDatabase_Log,
FILENAME = 'D:\CCTB111\MYCCTB111.LDF',
SIZE = 2MB,
MAXSIZE = 5MB)

● The LDF file should be on a separate drive / location as it can be used to recover a
corrupt MDF file.

2. What are the Differences between RDBMS and a Spreadsheet application?

● Spreadsheets are flat. RDBMS are more robust


● RDBMS can handle more data than spreadsheets far more efficiently

3. What are the Referential Integrity Rules?

● Constraint <constraint_name> <constraint_type> <name> references <Table(key)>


○ Foreign key
○ Primary key
○ Check
○ Default

4. What are Primary Key, Foreign Key and Index? Why would you use index in a database?

● Primary Key is a column in a table that accepts unique values for each row
● A Foreign Key is a Primary Key referenced from another table
● An Index is a lookup table that is used to speed up database searching

5. What is a JOIN? Describe how the JOIN works?

● JOIN adds another table to a query


● JOIN requires matching columns, specified by <table>.<column> e.g.
○ select student.StudentID, fullname, CourseID, Mark from student JOIN grade ON
student.StudentID = grade.studentID

6. What is a Subquery? Write a Subquery that displays Students Info (StudentID, CourseID and
Marks) of Students with Marks below the Class Average Mark:

● Subqueries use information gathered within an initial query


use CCTB111
select *
from grade
where mark > (select avg(mark) from grade)

7. What is an Aggregate Query? Name 4 Aggregate Functions and a very brief description

● An aggregate query takes data from a column and provides a result from it based on the
data
○ Avarage = avg()
○ Maximum value = max()
○ Minimum value = min()
○ Number of records count()

8. What SQL Statements do you use to Populate Tables, Delete Rows, and Update Rows?

● Populating tables can be done by


○ Manual entry
○ INSERT
○ Importing an external file
● Deleting Rows can be done by
○ DELETE FROM table_name WHERE condition;
● Updating Rows can be done by
○ UPDATE table_name
○ SET column1 = value1, column2 = value2, ...
○ WHERE condition;

9. What is a View? What are the advantages of Views (very briefly)?

● A View is a “Virtual Table”, which is sort of a copy of an actual table that can be viewed
and altered by specific people with specific security permissions

10. What is a Calculated Field? Provide 2 SQL formulas for calculating the AGE?

● A calculated field is a field whose value is derived from performing an operation


on one or more other fields
● select * , Age = year(getdate()) - year(birth_date)
from STUDENT
where birth_date is NOT NULL
order by age
● select * , Age = year(getdate()) - year(birth_date)
from STUDENT
where DATEDIFF(year,birth_date,getdate())>=40

11. Describe Wildcard Characters in SQL. What Operator should they be used with?

● * means “all”
○ Used in SELECT
● % means “any number of characters after this character”
○ Used in LIKE
● _ means “any single character”
○ Used in LIKE

You might also like