Question

In: Computer Science

Question 6        (Marks: 35) Structured Query Language (SQL) is a language that is widely used in...

Question 6        (Marks: 35) Structured Query Language (SQL) is a language that is widely used in industry to create, update and query data in relational databases. This question must NOT be done practically (i.e. in the computer room). You are required to write the SQL code in your answer book. Q.6.1 The below sample data in third normal form was provided by a database designer. Answer the below questions using this data. Table: Country Primary key: CountryID (auto number) All fields are mandatory CountryID Name Abbreviation CallingCode 1 South Africa ZA 27 2 Lesotho LS 266 3 Namibia NA 264 4 Egypt EG 20 Table: President Primary key: PresidentID (auto number) Foreign key: CountryID (mandatory) All fields are mandatory PresidentID CountryID Name Surname Year 1 1 Cyril Ramaphosa 2018 2 1 Jacob Zuma 2009 3 3 Hage Geingob 2015 4 3 Hifikepunye Pohamba 2005 18; 19; 20                       2020 © The Independent Institute of Education (Pty) Ltd 2020   Page 10 of 11    Q.6.1.1 Write a SQL statement to create the table President. Hint: The sample data should give you an indication of the data types you should use. (5)      Q.6.1.2 Write a SQL statement that will count the number of presidents that were inaugurated after 2009. (4)      Q.6.1.3 Write a SQL statement to insert the below row into table Country. CountryID Name Abbreviation CallingCode 5 Botswana BW 267 (4)      Q.6.1.4 Write a SQL statement to get the list of all the countries from the database, in alphabetical order by country name. Include all the columns from the Country table. (3)      Q.6.1.5 Write a SQL statement to get the list of all the presidents with a surname starting with the letter R. Include all fields from the President table. (3)      Q.6.1.6 Write a SQL statement to get the list of all the presidents, showing only the name and surname of the president, and the name of their country. (5) Q.6.2 What is the difference between the WHERE and HAVING clauses in SQL statements? (4) Q.6.3 What is the purpose of an index in a SQL database? (1) Q.6.4. The below ERD has been implemented in a SQL database. What will the result be of each of the below queries? Provide an explanation for your answer.   18; 19; 20                       2020 © The Independent Institute of Education (Pty) Ltd 2020   Page 11 of 11 Q.6.4.1 (2)      Q.6.4.2 (2)      Q.6.4.3  

Solutions

Expert Solution

Assumption:

For the last question  Q.6.4. ERD is not given and the question is incomplete.

Q.6.1.1 Write a SQL statement to create the table President.

     CREATE TABLE President (
        PresidentID        INT           NOT NULL ,                    
        CountryID          INT           NOT NULL,     
        Name               VARCHAR2(50)  NOT NULL,      
        Surname            VARCHAR2(50)  NOT NULL,
        Year               INT           NOT NULL,      
        CONSTRAINT President_PK PRIMARY KEY(PresidentID),
        CONSTRAINT President_FK1 FOREIGN KEY(CountryID) REFERENCES Country(CountryID)
        );

The table is craeted with the mentioned Primary Key and Foreign Key and the datatypes are gives based on the data that we have.

Q.6.1.2 Write a SQL statement that will count the number of presidents that were inaugurated after 2009. (4)

select * from President where year > 2009;

We have two records for the presidents who were inaugurated after 2009.

select count(*) from President where year > 2009;

Since it is mentioned 'After 2009',Records of 2009 should not be included.

Q.6.1.3 Write a SQL statement to insert the below row into table Country.
CountryID Name Abbreviation CallingCode
5 Botswana BW 267 (4)   

INSERT INTO Country
VALUES(5,'Botswana','BW',267);

Q.6.1.4 Write a SQL statement to get the list of all the countries from the database, in alphabetical order by country name.
Include all the columns from the Country table. (3)

select * from country order by name ;

'Order by country' indicates that the query has to be sorted by increasing alphabetical order of Country names.No need to specify 'ASC' for Ascending.

Q.6.1.5 Write a SQL statement to get the list of all the presidents with a surname starting with the letter R.
Include all fields from the President table. (3)

     select * from president where surname like 'R%' ;


'R%' indicates that the surname should start with R.'%' represents any number of any characters.


Q.6.1.6 Write a SQL statement to get the list of all the presidents, showing only the name and surname of the president, and the name of their country. (5)

select p.name,surname,c.name 
from
country c,president p
where c.countryid=p.countryid;

The country and President tables have countryID as common since countryID in president table refers to the countryID in Country table.

So both the tables can be joined with the common column to fetch columns from both the tables.


Q.6.2 What is the difference between the WHERE and HAVING clauses in SQL statements? (4)

Both HAVING and WHERE are used for filtering the query based on certain condition.

WHERE can be used in any queries where a filteration is needed based on a particular condition.

HAVING can be used only with GROUPED elements(to the rows that resulted as a part of grouping).That is,The query should have a GROUP BY clause .

A query can have both WHERE and HAVING clause at a time.

Q.6.3 What is the purpose of an index in a SQL database? (1)

Indexes are mainly used to speedup the querying time.When you use Index in a database you dont have to search each and every row to locate data.

Indexes make querying efficient and faster enhancing the entire system.


Related Solutions

Match each term to its correct description. Data-driven fraud detection Risk assessment Structured query language Data...
Match each term to its correct description. Data-driven fraud detection Risk assessment Structured query language Data analysis software Data warehouse Benford's law Vertical Analysis Horizontal analysis z-score Matosas matrix A. Tool that determines the percentage change in balance sheet and income statement numbers from one period to the next. B. A hypothesis testing approach: the investigator makes hypotheses and tests to see which are supported by the data. C. A standardized programming language that allows one to request information from...
6.         Question 6 [Total: 20 marks]                             &
6.         Question 6 [Total: 20 marks]                                                     Please discuss how “variation margin” and “margin call” are related in the context of daily settlement procedure.                                                                                [10 marks] b) What are the most important aspects of the design of a new futures contract?      [10 marks]
Question 01: Describe the most widely used techniques of family therapy.
Question 01: Describe the most widely used techniques of family therapy.
Question 2 (20 Marks) 2.1. Computers provide support in making structured decisions. This is achieved through...
Question 2 2.1. Computers provide support in making structured decisions. This is achieved through the use of Decision Support Systems (DSS) and Intelligent Support Systems (ISS), explain this in detail. 2.2. Explain the four-stage planning model for Strategic Information Technology Planning.
Question Three: ............................................... ...................................... (35 marks) The following is selected financial information in Jordanian dinars and...
Question Three: ............................................... ...................................... The following is selected financial information in Jordanian dinars and additional data for Al-Hikma Prepare the statement of cash flows for the year ended 12/31/2012 according to the indirect method 2011 2012 Land 58,800 21,000 Equipment 504,00 789,600 Compound depreciation (84,000) (115,600) Stock 168,000 201600 Accounts receivable (net) 84,000 151,200 Cash 42,000 63,000 Total 772 800 1110 800 The capital is 420,000 487,200 Retained earnings 67200 205800 Payable notes - long-term 168,000 302400 Payable notes -...
Question 3​(4 marks) It is believed that 35% of the population approves the current government. A...
Question 3​ It is believed that 35% of the population approves the current government. A poll is conducted prior to elections. a) Find the mean and standard error of the sample proportion, , of people approving the government. b) What is the sampling distribution of ? State any assumptions you need to make. c) A random sample of people were asked if they approve the government. What is the probability that at least 150 out of the 400 replied ‘yes’,...
QUESTION 2: STOCK CHARACTERISTICS AND VALUATION (35 MARKS) a) Compute the value of a share of...
QUESTION 2: STOCK CHARACTERISTICS AND VALUATION a) Compute the value of a share of common stock of Lexus Hotel Berhad whose most recent dividend was RM2.50 and is expected to grow at 3.50 percent per year for the next 5 years, 5 percent per year for the next 3 years, after which the dividend growth rate will increase to 6 percent per year indefinitely. Assume 10.00 percent required rate of return. b) Glass Art Manufacturing Berhad has a beta of...
Question 6 Question 2 Part (i) 6 marks You are working on the audit of Chip...
Question 6 Question 2 Part (i) 6 marks You are working on the audit of Chip Ltd and have completed your review of controls over cash receipts. You have noted the following controls: Control 1: Handling of cash is entirely separate from the accounting department. Control 2: Before the accounting department posts sales invoices to the accounts receivable master file, cash receipts are matched to specific sales invoices. Control 3: The accounting department has a procedure whereby all recorded cash...
Answer the following questions. Question 1 Centrifuge is a device that widely used in laboratory to...
Answer the following questions. Question 1 Centrifuge is a device that widely used in laboratory to separates particles from a solution. (a) Based on your knowledge about circular motion, briefly explain the working principle of the centrifuge on these separation process.    (b) State two types of centrifuge. Describe their specification and functions.   Question 2 (a) An object with a mass of 0.5 kg is put into a liquid which fill up in a container. In this process, the liquid displaced...
it is a question of discrete math RSA is the most widely used public key cryptosystem....
it is a question of discrete math RSA is the most widely used public key cryptosystem. In this discussion, you will apply RSA to post and read messages. For this reflection discussion, use the prime numbers p = 3 and q = 11. Using the public key e = 3, post a phrase about something that you found interesting or relevant in this course. Include only letters and spaces in your phrase. Represent the letters A through Z by using...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT