Question

In: Computer Science

Write a SQL statement which joins the parts table with the supplier table and lists the...

  1. Write a SQL statement which joins the parts table with the supplier table and lists the part_name, supplier_name for all parts in the part table. The supplier_id column in the suppliers table is the primary key in the suppliers table, and this key has been exported to the parts table where it is a foreign key. You should use an inner join for this query.

  1. Write a SQL statement which joins the parts table with the suppliers table and lists the part_name, supplier_name. You should return all rows from the parts table whether or not there are corresponding rows in the supplier table. You should use an outer join for this query.

  1. Write a SQL statement which joins the parts table with the supplier table and lists the part_no, part_name, supplier_name from parts and suppliers table. Only return the parts supplied by ‘Fred Smith ..'. You should use an inner join for this query.

Solutions

Expert Solution

Fo this question we have following two tables:-

1> supplier table (parent table):-

2> parts tabble (Child table) :-

1>We have to fetch the part_name and supplier_name from part table and supplier table using inner join query for first SQL statement. Now we understand how to fetch the above data step by step using inner join.

STEP 1 : First select the part_name from the parts table.

Query :- SELECT part_name from parts;

STEP 2 :then select the supplier_name from the supplier table.

Query :- SELECT supplier_name from supplier.

STEP 3:- Now merge above two queries using inner join and fetch data according to the supplier_id.

Final Query:-

SELECT P.part_name, S.supplier_name from parts as P inner join supplier as S on S.supplier_id = P.supplier_id;

Output Of First SQL statement:-

From above output, we can see that we get the part_name and supplier_name from both the table where part.supplier_id = supplier.supplier_id.

2> For second SQL statement we need to fetch all the parts table whether or not there are corresponding rows in the supplier table. SO here we can used left join which is type of Outer join.

Now, we apply left join on supplier table so we can get the all data of parts table because parts table contain supplier_id as foreign key and the data that are not matched in pat table we get the NULL data int that rows.

STEP 1 : First select the part_name from the parts table.

Query :- SELECT part_name from parts;

STEP 2 :then select the supplier_name from the supplier table.

Query :- SELECT supplier_name from supplier.

STEP 3: Now merge STEP1 and STEP 2 query using left join which is type of outer join.

Final Query:-

SELECT P.part_name, S.supplier_name from supplier as S left join parts as P on S.supplier_id = P.supplier_id;

Output Of Second SQL statement:-

From the above output, we can see that we get all the part data using left join because we have foriegn key supplier_id and we get the null data in part_name column because that are does not match with supplier_id.

Because "Sagar Naik" supplier does not suuply any part so parts table does not have any record so we get the null data after left join.

3> For third SQL statement, we need to fetch the part_no, part_name and supplier_name from the parts and supplier table using ineer join that are used in first SQL statement and here we have to fetch data for the "Fred Smith" supplier name. So we need to use WHERE clause.

STEP 1 : First select the part_no,part_name from the parts table.

Query :- SELECT part_no,part_name from parts;

STEP 2 : then select the supplier_name "Fred Smith" from the supplier table using WHERE clause.

Query :- SELECT supplier_name from supplier where supplier_name='Fred Smith'.

STEP 3: Now merge STEP1 and STEP 2 query using left join which is type of outer join.

Final Query:-

SELECT P.part_no,P.part_name, S.supplier_name from parts as P inner join supplier as S on S.supplier_id = P.supplier_id WHERE S.supplier_name = 'Fred Smith';

Output Of Third SQL statement:-

From the above output, we can see that we get the all parts number and name that are supplied by "Fred Smith" supplier.

I hope you will understand above queries and the concept of inner join with WHERE clause as well as outer join.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

Write a SQL statement which joins the parts table with the supplier table and lists the...
Write a SQL statement which joins the parts table with the supplier table and lists the part_name, supplier_name for all parts in the part table. The supplier_id column in the suppliers table is the primary key in the suppliers table, and this key has been exported to the parts table where it is a foreign key. You should use an inner join for this query. Write a SQL statement which joins the parts table with the suppliers table and lists...
1. Write a SQL statement which joins the rider_student table with the rider_major table and lists...
1. Write a SQL statement which joins the rider_student table with the rider_major table and lists the rider student name and the name of the major (major_name) and the description of the major for which they are currently assigned. (You may use the SQL 'join' subclause, or simply express the join as part of the 'where' clause by indicating that you only want records where the primary key of the child table, rider_major, equals the corresponding foreign key of the...
1. What is one of the most powerful features of SQL? Operators Functions Table Joins 2....
1. What is one of the most powerful features of SQL? Operators Functions Table Joins 2. A JOIN is a mechanism used to associate tables within a SELECT statement? True False 3. A JOIN can modify existing database tables True False None of the above 4. The WHERE clause acts as a filter to only include rows that match the filter condition. True False 5. The more tables you JOIN the less resources the system uses. True False 6. The...
Use a single SQL statement to create a relational table and to load into the table...
Use a single SQL statement to create a relational table and to load into the table department name, subject code, year of running and session of running that offered by the departments. Note that a running subject offered by a department means a lecturer of the department has been assigned to teach the subject. Next, enforce the appropriate consistency constraints on the new table.    When ready use SELECT statement to list the contents of the relational table created and...
1. Can you please describe the different kinds of joins used in SQL? 2. What SQL...
1. Can you please describe the different kinds of joins used in SQL? 2. What SQL syntax would you use to ensure you are not returning duplicates in your data sets? 3. What SQL syntax would you use to change data in a table if you had write access to the table? Having write access means the data manipulation would occur in the table being pulled from. 4. What SQL syntax would you use to change data in a table...
write a sql statement that retrieves product ID (ProductID) and name (Name) from Production.product table for...
write a sql statement that retrieves product ID (ProductID) and name (Name) from Production.product table for all product whose name includes both the words "silver" and "frame"
Can you please describe the different kinds of joins used in SQL?
Can you please describe the different kinds of joins used in SQL?
Write an SQL statement to show which customers boughtwhich items, and include any items that...
Write an SQL statement to show which customers bought which items, and include any items that have not been sold. Include LastName, FirstName, InvoiceNumber, InvoiceDate,ItemNumber, ItemDescription, ArtistLastName, and ArtistFirstName. Use a join using JOIN ON syntax, and sort the results by ArtistLastName and ArtistFirstName in ascending order (Hint: you have to use a RIGHT JOIN on the last step).
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as if it were being added to MySQL (please give explanations for each line of SQL code and a copy of the code as it would be entered into the query by itself: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT