Database query problem
the questions 2,5 and 6 of this sample paper
question 2 is not written properly, it should be product name ,
supplier name which supplies maximum quantity of each
product
Qo.
ANS:
Let's Create DB first and their structure and then join tables them by INNER JOIN.
Product Databse1 Structure: IMAGE:

DATA IMAGE:

Supplier Databse2 Structure: IMAGE:

DATA IMAGE:

Supply Databse3 Structure: IMAGE:

DATA IMAGE:

Now comes to query(Questions)
NOTE: I am using PHP MySQL DB, but queries are same in SQL DB, So focus on the query rather than medium).
Qo2: get the product name, supplier name which supplying maximum quantities of each product.
ANS:
QUERY
IS: "SELECT prid,prname,supname,qty FROM product
INNER JOIN supply on product.prid=supply.pid INNER JOIN supplier on
supplier.supid=supply.sid order by qty desc";
PHP CODE IS:
<html>
<head>
<title>Show Data</title>
<style>
table{
border-collapse:
collapse;
width:
100%;
}
tr,td{
padding:
15px;
text-align:
center;
}
</style>
</head>
<?php
$product_db=mysqli_connect("localhost","root","","stationary") or
die("Can't connect to db");
$product_query="SELECT prid,prname,supname,qty FROM
product INNER JOIN supply on product.prid=supply.pid INNER JOIN
supplier on supplier.supid=supply.sid order by qty desc";
$product_result=mysqli_query($product_db,$product_query);
echo "<table border='1'>";
echo "<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Supplier Name</th>
<th>Quantity(s)</th>
</tr>";
while($product_ans=mysqli_fetch_assoc($product_result))
{
echo "<tr>
<td>".$product_ans['prid']."</td>
<td>".$product_ans['prname']."</td>
<td>".$product_ans['supname']."</td>
<td>".$product_ans['qty']."</td>
</tr>";
}
echo "</table>";
?>
you can find max using max(qty).
OUTPUT IMAGE:

Q5) write a function that takes the product id as input and return supplied quantity.
ANS: Function is:
function getQuantity($prod_id)
{
$product_db=mysqli_connect("localhost","root","","stationary") or
die("Can't connect to db");
$product_query="SELECT * FROM
product INNER JOIN supply on product.prid=supply.pid where
prid=$prod_id";
$product_result=mysqli_query($product_db,$product_query);
$product_ans=mysqli_fetch_assoc($product_result);
$totalQty=$product_ans['qty'];
return $totalQty;
}
echo "Quantity is:
".getQuantity(104).'<br/>';
echo "Quantity is:
".getQuantity(105).'<br/>';
Qo6)
ANS: Function with PHP Code is:
<html>
<head>
<title>Show Data</title>
<style>
table{
border-collapse:
collapse;
width:
100%;
}
tr,td{
padding:
15px;
text-align:
center;
}
</style>
</head>
<?php
function getQuantity($prod_id)
{
$product_db=mysqli_connect("localhost","root","","stationary") or
die("Can't connect to db");
$product_query="SELECT * FROM
product INNER JOIN supply on product.prid=supply.pid INNER JOIN
supplier on supplier.supid=supply.sid where prid=$prod_id ";
$product_result=mysqli_query($product_db,$product_query);
if(mysqli_num_rows($product_result)>0)
{
echo "<table
border='1'>";
echo "<tr>
<th>Product
ID</th>
<th>Product
Name</th>
<th>Supplier
Name</th>
<th>Quantity(s)</th>
</tr>";
while($product_ans=mysqli_fetch_assoc($product_result))
{
echo "<tr>
<td>".$product_ans['prid']."</td>
<td>".$product_ans['prname']."</td>
<td>".$product_ans['supname']."</td>
<td>".$product_ans['qty']."</td>
</tr>";
}
echo
"</table>";
}
else{
echo "No Product
Supplied";
}
}
getQuantity(104);
getQuantity(105);
?>
OUTPUT IMAGE:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
THANKS!!
Database query problem the questions 2,5 and 6 of this sample paper question 2 is not written properly, it should be product name , supplier name which supplies maximum quantity of each product Produc...