PBL Week 4_ MN405

.docx

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

Report
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