You are on page 1of 14

Questions:

1. What does * mean in a SELECT statement?

It will select all coloumns. When (*) is used, it will select all in your database instead

of selecting coloumns manually in case you need to perform an operation on everything

combined you can use (*) in Select statement.

2. What is the purpose of a JOIN Clause? How many JOIN clauses are there?

What is the difference between a RIGHT JOIN and a LEFT JOIN?

Join clause is used to merge Rows from two or more than two tables utilizing the

relation between them, its purpose is to fetch data from multiple tables in a single query,

enabling the creation of meaningful sets.

There are 4 types of Join Clause

1)INNER JOIN: Returns records that have matching values in both tables

2)LEFT JOIN: Returns all records from the left table, and the matched records

from the right table

3)RIGHT JOIN: Returns all records from the left table, and the matched records

from the right table

4)FULL JOIN: Returns all records when there is a match in either left or right table,

if no match is found, NULL values are returned.

Also, I found online a 5th type of join clause the:

5)CROSS JOIN: It is used to Return the Cartesian Product of 2 Tables


The Primary deference between Right join and Left Join is

RIGHT JOIN: It includes all the Element from The Right Table and all the matched

rows from the left table is obtained

LEFT JOIN: It includes all the Element from The Left Table and all the matched

rows from the Right table is obtained

Graphic below helps to understand the 5 types of Join Clause

3. How is the WHERE statement different than the GROUP BY statement?

SQL WHERE Statement is used to filter records. It is used to extract only those

records that fulfill a specified condition. For example:

Select all customers from Mexico:


SELECT * FROM Customers

WHERE Country='Mexico';

On the other hand, SQL Group By statement groups rows that have the same

values into summary rows, like "find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions

(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

4. What is the order that the SQL processor will evaluate SQL

statements? How is this different than how the statement is written?

The order that the SQL processor will evaluate the SQL statement is in this

following order

1. FROM

2. WHERE

3. GROUP BY

4. HAVING

5. SELECT

6. DISTINCT

7. ORDER BY

8. LIMIT/OFFSET
The difference is that it is referred as the logical processing order, and even though

you write the square statements in a selected order, the square processor will reorganize

and optimize the execution and it is primarily based on the database's internal common

sense to fetch and procedure records efficaciously.

5. In the textbook, there are some 'keys' that are discussed. Foreign keys and

primary keys are important. What are these and how are they used in database design?

In a database design, Primary keys and foreign keys are essential concepts for

setting up relationships between tables.

1) The PRIMARY KEY constraint uniquely identifies each record in a table.

Primary keys must contain UNIQUE values, and cannot contain NULL values.

A table can have only ONE primary key; and in the table, this primary key can

consist of single or multiple columns (fields). It guarantees that every row in a table may

be uniquely identified through a selected column or set of columns.

The FOREIGN KEY constraint is used to prevent actions that would destroy links

between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to

the PRIMARY KEY in another table.

The table with the foreign key is called the child table, and the table with the primary

key is called the referenced or parent table. In other words, primary keys uniquely pick

out statistics inside a table, and overseas keys establish relationships between tables by

linking to the Primary keys in different tables, selling records integrity and facilitating

efficient information retrieval.

6. From the Hospital Database what SQL code will pull the Doctors Last Name

and their Specialty?

SELECT last_name, Specialty

FROM doctors;
7. From the Hospital Database what SQL code will pull just the Doctors Unique

ID?

SELECT doctor_id

FROM doctors;

8. Is the Doctor's Unique ID a Key in the Doctors Table? If so, what kind?

Yes, it is most likely to be and Primary key because Unique ID needs to be Unique

and non-repetitive.

9. The following code is a basic two table join format. You can use this format to

answer the questions that require joins (below).


SELECT column_name(s)

FROM table1

LEFT JOIN table2

ON table1.column_name = table2.column_name;

Describe the steps that are occurring based on the lecture slide that deals with

SQL processor operations. What is happening in the processor first? What is happening

in the processor second? What is happening in the processor third?

The SQL code provided initiates with the SQL processor identifying the involved

tables, denoted as table1 and table2. Subsequently, the processor executes a LEFT

JOIN operation, merging rows from table1 with matching rows from table2 based on the

specified condition (table1.column_name = table2.column_name). All rows from

table1 are included in the outcome, and if there are corresponding rows in table2, they

are incorporated as well, with NULL values filling in for unmatched columns. Finally, the

processor selects the specified columns (column_name(s)) from the resulting dataset.

This sequence mirrors the logical order of SQL processor operations, starting with the

identification of tables, followed by the JOIN operation, and concluding with the selection

of desired columns from the joined dataset.

The Order of Steps are

1) FROM CLAUSE

it identifies both the table


2)LEFT JOIN CLAUSE

The left join Operation is Performed ('table1.column_name = table2.column_name')

3)SELECT CLAUSE

Once the join Operation is Performed it selects the specified column.

9. What SQL code would JOIN admissions table information with patient

information?

SELECT *

FROM admissions
INNER JOIN patients ON admissions.patient_id = patients.patient_id;

10. If you were to modify that SQL code to provide just patient last name and

diagnosis what code would provide only those two fields in the SQL viewer?

SELECT patients.last_name, admissions.diagnosis

FROM admissions

INNER JOIN patients ON admissions.patient_id = patients.patient_id;


11. If you were to modify that SQL code to provide last name, diagnosis, and

birth date, what code would provide only those three fields?

SELECT patients.last_name, admissions.diagnosis, patients.birth_date

FROM admissions

INNER JOIN patients ON admissions.patient_id = patients.patient_id;


13. For 11 and 12 (above), how many fields are present if the * is used in the

SELECT statement?

If Using (*) in the SELECT statement in both cases it would retrieve all columns

from both the "patient" and "admissions" tables. Consequently, the number of fields

would be equivalent to the total count of columns present in both tables.

14. For 11 and 12 (above), how many records are present when the JOIN is

implemented?

The number of records produced by the JOIN operation is dependent on the data

stored in your database. The size of the result set is determined by the matching rows
between the "admissions" and "patient" tables, as specified in the JOIN condition

(admissions.patient_id = patients.patient_id).

In cases where a patient has multiple admissions, there may be several

corresponding rows in the "admissions" table, and each admission record would be

matched with the respective patient details during the JOIN. The final count of records is

influenced by how many admission records align with patients in the "patient" table.

To determine the exact record count, you need to execute the SQL query on your

specific database. If you want to obtain a count without retrieving all the details, you can

use the COUNT() function in your SQL query, as illustrated in the provided examples.

To retrieve only the patient's last name and diagnosis:

1) TO Retrive Just patient last Name and Diagnosis

SELECT patients.last_name, admissions.Diagnosis

FROM admissions

INNER JOIN patients ON admissions.patients_id = patients.patient_id;

2) TO Retrive Last Name, Diagnosis and Birth Date

SELECT patients.last_name, admissions.Diagnosis, patients.birth_date

FROM admissions

INNER JOIN patient ON admissions.Patient_id = patients.patient_id;


15. In the Northwind Database, category_id (from categories) is related to the

products table. First, look at the schematic and explain how its related based on the

chapter readings. Second, what code would JOIN these two tables?

In the Northwind Database, the link between the "categories" table and the

"products" table is established through the utilization of the category_id column. This

adheres to a standard practice in relational databases, where a column in one table (in

this case, the category_id in the "products" table) references the primary key column in

another table (specifically, the primary key category_id in the "categories" table). This

linkage is commonly known as a foreign key relationship. A brief explanation is as

follows: • Foreign Key Relationship: In the "products" table, the category_id column

serves as a foreign key, pointing to the primary key in the "categories" table.

Consequently, each entry for a product in the "products" table is associated with a

specific category as defined in the "categories" table. To merge these two tables, a

typical SQL query involving the JOIN operation, specifically an INNER JOIN based on

the shared column (category_id), would be employed.

16. How would you view Category Name and the Supplier ID in the SQL viewer?

To display the Category Name and Supplier ID in the SQL Viewer, you can utilize

the following code

SELECT category_name, supplier_id


FROM categories;

This SQL query fetches the specified columns ("category_name" and

"supplier_id") from the "categories" table. Adjust the column names as needed to match

the actual names used in your database.

You might also like