PBL Week 4_ MN405
.docx
keyboard_arrow_up
School
Royal Melbourne Institute of Technology *
*We aren’t endorsed by this school
Course
405
Subject
Computer Science
Date
May 21, 2024
Type
docx
Pages
5
Uploaded by MasterJayPerson1109
PBL WEEK 4
MN405
DATA RETRIEVAL WITH SQL QUERIES
1.
The SELECT statement is the main data retrieval command in SQL. Write the syntax of
SELECT statement and explain each clause.
The SELECT statement in SQL is used to retrieve data from one or more tables in a database. The syntax of SELECT statement is as follows:
SELECT columnlist
FROM tablelist;
[WHERE conditionlist ]
[HAVING conditionlist ]
[ORDER BY columnlist [ASC | DESC] ];
Clauses:
1.
SELECT
clause: This clause specifies the columns you want to retrieve from the database table.
2.
FROM
clause: This clause specifies the name of the table from which you want to retrieve data.
3.
WHERE
clause: This clause filters the results based on a specific condition. You can use comparison operators and logical operators to create complex filtering criteria.
4.
ORDER BY
clause: This clause sorts the retrieved data based on one or more columns. You can specify ascending (ASC) or descending (DESC) order for sorting.
5.
GROUP BY
clause: This clause groups the retrieved data based on one or more columns. This is often used in conjunction with aggregate functions (like SUM, COUNT, AVG) to perform calculations on grouped data.
6.
HAVING clause: This clause filters groups created by the GROUP BY clause based on a condition. It's similar to the WHERE clause but applies to groups instead of individual rows.
2.
Explain why the following command would create an error and what changes could
be made to fix the error: SELECT V_CODE, SUM(P_QOH) FROM PRODUCT;
The above SQL command tries to retrieve the V_CODE column and the total of the P_QOH columns from the PRODUCT table without using a GROUP BY clause. This command will most likely return an error because when using an aggregate function like SUM (), you must normally specify a GROUP BY clause to specify how to group the data for aggregating purposes.
To fix the error, we need to either delete the non-aggregated column (V_CODE) from the SELECT list or include a GROUP BY clause that specifies which columns to group by.
3.
What three join types are included in the outer join classification?
The following are the 3 types of the outer join classification
Left Outer Join: This join includes all rows from the left table (the table mentioned first in the
JOIN statement) and matching rows from the right table. For rows in the left table that don't have a match in the right table, the corresponding columns from the right table will be filled with NULL values.
Right Outer Join: This join functions similarly to the left outer join, but in reverse. It includes all rows from the right table and matching rows from the left table. Unmatched rows from the left table will have NULL values in their corresponding columns in the result set.
Full Outer Join: This join combines the results of both left and right outer joins. It includes all rows from both tables, regardless of whether there's a match in the other table. Unmatched rows from either table will have NULL values in their corresponding columns.
4.
Explain the difference between an ORDER BY clause and a GROUP BY clause.
ORDER BY sorts the full result set based on specified columns, whereas GROUP BY groups the result set into summary rows to do aggregate calculations.
5.
What is the difference between the COUNT aggregate function and the SUM aggregate function?
COUNT counts the number of rows, whereas SUM calculates the entire sum of values in a column.
6.
In a SELECT query, what is the difference between a WHERE clause and a HAVING clause?
The WHERE clause filters rows before any grouping or aggregation occurs, but the HAVING clause filters groups of rows after they have been grouped by the GROUP BY clause and aggregated.
7.
What is a subquery, and what are its basic characteristics? What are the three types of results that a subquery can return?
A subquery is a complex SQL feature that allows you to encapsulate an entire SELECT statement inside another SELECT statement. It functions as a mini query, retrieving data used by the outer query.
The following are the basic characteristics of subquery.
Nested within another SQL statement:
A subquery is surrounded in parenthesis and placed within the WHERE, HAVING, FROM (less common), or JOIN clauses of the outer query.
Provides data for the outer query
: The results of the subquery are used to filter, compare, or
merge data from the outer query.
Can be simple or complex: Subqueries can range from simple comparisons to many clauses and joins.
The following are the three types of results that a subquery can return.
Single Value:
The subquery can return a single value, such as a minimum, maximum, average, or count, which is then used in a comparison with the outer query.
Single Row:
The subquery can return a single row containing multiple columns. This row is then compared with the outer query based on specific columns.
Multiple Rows and Columns:
The subquery can return a result set with multiple rows and columns, which is then used for comparisons or joins with the outer query.
8.
Write the following queries in SQL, using the university schema.
(1)
Find the IDs, names, and department names of all students.
SELECT studID, studName, deptName
FROM student;
(2)
Find the names and salaries of all instructors.
SELECT insName, salary
FROM instructor;
(3)
Find the titles of all courses offered in the ‘Info. Tech.’ department.
SELECT title
FROM course
WHERE deptName = 'Info. Tech.';
(4)
Find the titles of courses in the IT department that have 3 credits. SELECT title
FROM course
WHERE deptName = 'Info. Tech.' AND credits = 3 ;
(5)
Find the IDs of all students who were taught by an instructor named ‘Dr. Miah’; make sure there are no duplicates in the result.
SELECT DISTINCT takes.studID
FROM takes
JOIN teaches ON takes.courseID = teaches.courseID AND takes.secID = teaches.secID AND takes.semester = teaches.semester AND takes.year = teaches.year
JOIN instructor ON teaches.insID = instructor.insID
WHERE instructor.insName = 'Dr. Miah';
(6)
Find the highest salary of any instructor. SELECT MAX(salary) AS highest_salary
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
J
SHORTAND NOTATION FOR RELATIONAL SQL TABLES
Notation Example Meaning
Underlined A or A, B The attribute(s) is
(are) a primary key
Superscript name
of relation
AR or AR, BR The attribute(s) is
(are) a foreign key
referencing
relation R
As an example, the schema
R(A, B, C, D, ES)
S(F, G, H)
corresponds to the following SQL tables:
CREATE TABLE R
( A <any SQL type>,
B <any SQL type>,
C <any SQL type>,
D <any SQL type>,
E <any SQL type>,
PRIMARY KEY(A),
FOREIGN KEY (E) REFERENCES S(F)
);
CREATE TABLE S
( F <any SQL type>,
G <any SQL type>,
H <any SQL type>,
PRIMARY KEY(F))
EXERCISE
Consider the following relational schema, representing five relations describing shopping transactions and
information about credit cards generating them [the used notation is explained above].
SHOPPINGTRANSACTION (TransId, Date, Amount, Currency, ExchangeRate, CardNbrCREDITCARD, StoreIdSTORE)
CREDITCARD (CardNbr, CardTypeCARDTYPE, CardOwnerOWNER, ExpDate, Limit)…
arrow_forward
SBN Title Author 12345678 The Hobbit J.R.R. Tolkien 45678912 DaVinci Code Dan Brown Your student ID DBS311 Your Name
use the following statement to Write a PL/SQL Function that accepts 4 parameters, 2 strings representing students names, and 2 integers representing the marks they received in DBS311. The function will determine which student had the higher mark and return the name of the student. If the marks were the same, then return the word "same" and return "error" if an error occurs or the calculation can not be determined.
arrow_forward
relation:
Book (BookID, Author, ISBN, Title)
Write a PL/SQL block of code that performs the following tasks:
Read a Book id provided by user at run time and store it in a variable.
Fetch the Author and title of the book having the entered id and store them inside two variables
Display the fetched book author and title.
arrow_forward
Write a PL/SQL block that uses searched CASE statement to classify students based on their scores in a certain test according to the following table:
Score
Action
>= 90
A scholarship is given
80-89
A certificate is given
70-79
A training course is recommended
<70
The course should be repeated
Write the searched CASE structure only.
arrow_forward
SQL QUERIES
Employees(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID)Department(DEPARTMENT_ID,DEPARTMENT_NAME,MANAGER_ID,LOCATION_ID)A: Write a query in SQL to display the minimum, maximum salary and DEPARTMENT_ID of all thosedepartments whose minimum salary is greater than 4000 in ascending order.
B: Write a Query in SQL to display characters only from 2 to 5 of “Last_Name” from Employees table.
C: Write a query in SQL to display the HIRE_DATE of every employee in following format.Format: 11-july-2019
arrow_forward
SQL QUERIES
Employees(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID)Department(DEPARTMENT_ID,DEPARTMENT_NAME,MANAGER_ID,LOCATION_ID)A: Write a query in SQL to display the minimum, maximum salary and DEPARTMENT_ID of all thosedepartments whose minimum salary is greater than 4000 in ascending order.
arrow_forward
SQL query Knowledge
arrow_forward
sql queries
Employees(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE,JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID,DEPARTMENT_NAME)Department(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)Location(LOCATION_ID,STREET_ADDRESS,POSTAL_CODE,CITY,STATE_PROVINCE,COUNTRY_ID)
Write a query in SQL to display the Entire data of All those employees whoseDEPARTMENT_ID is greater than the DEPARTMENT_ID of all those employees who earn morethan 50,000 per month
Write a query in SQL to display the minimum, maximum salary and DEPARTMENT_NAMEof all those departments whose minimum salary is greater than 4000 in ascending order.
arrow_forward
SQL QUERIES
Employees(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE,JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID,DEPARTMENT_NAME)Department(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)Location(LOCATION_ID,STREET_ADDRESS,POSTAL_CODE,CITY,STATE_PROVINCE,COUNTRY_ID)
Write a Query in SQL to delete the record of all those cities where city name ends with b fromlocations table.
arrow_forward
SQL QUERIES
Employees(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE,JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID,DEPARTMENT_NAME)Department(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)Location(LOCATION_ID,STREET_ADDRESS,POSTAL_CODE,CITY,STATE_PROVINCE,COUNTRY_ID)
Write a query in SQL to display the DEPARTMENT_NAME of all those Departments whoseDEPARTMENT_ID is greater than the DEPARTMENT_ID of Physics department.
Write a Query in SQL to show the FIRST_NAME of all those employees whose salary is lessthan the salary of all those employees whose MANAGER_ID is 101 or 102.
arrow_forward
**In SQL**
Write a SELECT statement that returns these columns: InstructorDept The DepartmentName column from the Departments table for a related instructor LastName The LastName column from the Instructors table FirstName The FirstName column from the Instructors table CourseDescription The CourseDescription column from the Courses table CourseDept The DepartmentName column from the Departments table for a related instructor Return one row for each course that’s in a different department than the department of the instructor assigned to teach that course. (Hint: You will need to join the Departments table to both the Instructors table and the Courses table, which will require you to use table aliases to distinguish the two tables.)
arrow_forward
PL/SQL Question
I need to build pl/sql block that prompts a user for the number of visitors each day for the past 5 days and then displays the average number of visitors per day.
For example;
day 1: 19
day 2: 21
day 3: 23
...
The avg number of visitors is: ___
like this.
arrow_forward
Database design
Objectives:
To understand and be able to write simple SQL queries
Instruction:
Refer to HR schema to answer the questions
Include screenshot of your code and output of each question
1. Write SQL query that displays the last name and salary of employees earning more than $12,000.
2. Write SQL query that displays the last name and department number for employee number 176.
3. Write SQL query to display the last name and salary for all employees whose salary is not in the range $5,000 through $12,000.
4. Write SQL query to display the last name, job ID, and hire date for employees with the last names of Matos and Taylor. Order the query in ascending order by hire date.
5. Display the last name and department ID of all employees in departments 20 or 50 in ascending alphabetical order by name.
6. Write a query that displays the last names of all employees who have both an “a” and an “e” in their last name.
7. Write a query to display the employee…
arrow_forward
fa table in a SQL database has a column of type `VARCHAR(100)", what does the "100"
signify?
a) The number of records that can be stored in the column.
b) The number of unique characters that can be stored in the column.
c) The maximum length of a string that can be stored in the column.
d) The minimum length of a string that can be stored in the column.
arrow_forward
Write the code steps to do the following within SQL Create a database, this
database includes two tables, and each table holds specific columns, and one of
these columns is made a master key and the value of a specific column is unique
in the two tables. And then find the number of elements in one of the two tables,
if the number of elements is less than 5 find the difference between the two
tables, and if greater than 5 find the multiplication Product between the two
tables and Print a message explaining that
arrow_forward
**IN SQL **
1- Write a query clause to display the internal structure of the departments table, then write a query clause to display all its data, so that the result appears as follows: ** SEE PIC **
2- Write a query sentence to display the names, jobs, dates of appointment and employee numbers so that the employee number appears first
arrow_forward
SQL
arrow_forward
introduction to databases
Write PL/SQL code to create a function called best_ranked_post to list out all post which gets more than 50 likes.
arrow_forward
example inn 100 sqlmstatement
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781305627482
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781285196145
Author:Steven, Steven Morris, Carlos Coronel, Carlos, Coronel, Carlos; Morris, Carlos Coronel and Steven Morris, Carlos Coronel; Steven Morris, Steven Morris; Carlos Coronel
Publisher:Cengage Learning
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage
Related Questions
- J SHORTAND NOTATION FOR RELATIONAL SQL TABLES Notation Example Meaning Underlined A or A, B The attribute(s) is (are) a primary key Superscript name of relation AR or AR, BR The attribute(s) is (are) a foreign key referencing relation R As an example, the schema R(A, B, C, D, ES) S(F, G, H) corresponds to the following SQL tables: CREATE TABLE R ( A <any SQL type>, B <any SQL type>, C <any SQL type>, D <any SQL type>, E <any SQL type>, PRIMARY KEY(A), FOREIGN KEY (E) REFERENCES S(F) ); CREATE TABLE S ( F <any SQL type>, G <any SQL type>, H <any SQL type>, PRIMARY KEY(F)) EXERCISE Consider the following relational schema, representing five relations describing shopping transactions and information about credit cards generating them [the used notation is explained above]. SHOPPINGTRANSACTION (TransId, Date, Amount, Currency, ExchangeRate, CardNbrCREDITCARD, StoreIdSTORE) CREDITCARD (CardNbr, CardTypeCARDTYPE, CardOwnerOWNER, ExpDate, Limit)…arrow_forwardSBN Title Author 12345678 The Hobbit J.R.R. Tolkien 45678912 DaVinci Code Dan Brown Your student ID DBS311 Your Name use the following statement to Write a PL/SQL Function that accepts 4 parameters, 2 strings representing students names, and 2 integers representing the marks they received in DBS311. The function will determine which student had the higher mark and return the name of the student. If the marks were the same, then return the word "same" and return "error" if an error occurs or the calculation can not be determined.arrow_forwardrelation: Book (BookID, Author, ISBN, Title) Write a PL/SQL block of code that performs the following tasks: Read a Book id provided by user at run time and store it in a variable. Fetch the Author and title of the book having the entered id and store them inside two variables Display the fetched book author and title.arrow_forward
- Write a PL/SQL block that uses searched CASE statement to classify students based on their scores in a certain test according to the following table: Score Action >= 90 A scholarship is given 80-89 A certificate is given 70-79 A training course is recommended <70 The course should be repeated Write the searched CASE structure only.arrow_forwardSQL QUERIES Employees(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID)Department(DEPARTMENT_ID,DEPARTMENT_NAME,MANAGER_ID,LOCATION_ID)A: Write a query in SQL to display the minimum, maximum salary and DEPARTMENT_ID of all thosedepartments whose minimum salary is greater than 4000 in ascending order. B: Write a Query in SQL to display characters only from 2 to 5 of “Last_Name” from Employees table. C: Write a query in SQL to display the HIRE_DATE of every employee in following format.Format: 11-july-2019arrow_forwardSQL QUERIES Employees(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID)Department(DEPARTMENT_ID,DEPARTMENT_NAME,MANAGER_ID,LOCATION_ID)A: Write a query in SQL to display the minimum, maximum salary and DEPARTMENT_ID of all thosedepartments whose minimum salary is greater than 4000 in ascending order.arrow_forward
- SQL query Knowledgearrow_forwardsql queries Employees(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE,JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID,DEPARTMENT_NAME)Department(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)Location(LOCATION_ID,STREET_ADDRESS,POSTAL_CODE,CITY,STATE_PROVINCE,COUNTRY_ID) Write a query in SQL to display the Entire data of All those employees whoseDEPARTMENT_ID is greater than the DEPARTMENT_ID of all those employees who earn morethan 50,000 per month Write a query in SQL to display the minimum, maximum salary and DEPARTMENT_NAMEof all those departments whose minimum salary is greater than 4000 in ascending order.arrow_forwardSQL QUERIES Employees(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE,JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID,DEPARTMENT_NAME)Department(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)Location(LOCATION_ID,STREET_ADDRESS,POSTAL_CODE,CITY,STATE_PROVINCE,COUNTRY_ID) Write a Query in SQL to delete the record of all those cities where city name ends with b fromlocations table.arrow_forward
- SQL QUERIES Employees(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE,JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID,DEPARTMENT_NAME)Department(DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, LOCATION_ID)Location(LOCATION_ID,STREET_ADDRESS,POSTAL_CODE,CITY,STATE_PROVINCE,COUNTRY_ID) Write a query in SQL to display the DEPARTMENT_NAME of all those Departments whoseDEPARTMENT_ID is greater than the DEPARTMENT_ID of Physics department. Write a Query in SQL to show the FIRST_NAME of all those employees whose salary is lessthan the salary of all those employees whose MANAGER_ID is 101 or 102.arrow_forward**In SQL** Write a SELECT statement that returns these columns: InstructorDept The DepartmentName column from the Departments table for a related instructor LastName The LastName column from the Instructors table FirstName The FirstName column from the Instructors table CourseDescription The CourseDescription column from the Courses table CourseDept The DepartmentName column from the Departments table for a related instructor Return one row for each course that’s in a different department than the department of the instructor assigned to teach that course. (Hint: You will need to join the Departments table to both the Instructors table and the Courses table, which will require you to use table aliases to distinguish the two tables.)arrow_forwardPL/SQL Question I need to build pl/sql block that prompts a user for the number of visitors each day for the past 5 days and then displays the average number of visitors per day. For example; day 1: 19 day 2: 21 day 3: 23 ... The avg number of visitors is: ___ like this.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781305627482Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781285196145Author:Steven, Steven Morris, Carlos Coronel, Carlos, Coronel, Carlos; Morris, Carlos Coronel and Steven Morris, Carlos Coronel; Steven Morris, Steven Morris; Carlos CoronelPublisher:Cengage LearningCOMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE L
- Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781305627482
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781285196145
Author:Steven, Steven Morris, Carlos Coronel, Carlos, Coronel, Carlos; Morris, Carlos Coronel and Steven Morris, Carlos Coronel; Steven Morris, Steven Morris; Carlos Coronel
Publisher:Cengage Learning
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage