Point out the maker and speed of the laptops having hard drive capacity more or equal to 10 Gb - sql

I tried this in two ways (I think they are the same):
select product.maker,speed from product,laptop
where product.model=laptop.model and hd>='10'
and
select product.maker, speed from laptop
join product
on (product.model=laptop.model)
where hd>='10'
and I receive this in the simulator:
"Your query produced correct result set on main database, but it failed test on second, checking database. * Wrong number of records (more by 1)
This exercise has FAQ"
The relevant tables are:
Table Laptop
code model speed ram hd price screen
------------------------------------------------------
1 1298 350 32 4.0 700.0000 11
2 1321 500 64 8.0 970.0000 12
3 1750 750 128 12.0 1200.0000 14
4 1298 600 64 10.0 1050.0000 15
5 1752 750 128 10.0 1150.0000 14
6 1298 450 64 10.0 950.0000 12
Table Product:
maker model Type
-----------------------
A 1232 PC
A 1233 PC
A 1276 Printer
A 1298 Laptop
A 1401 Printer
A 1408 Printer
A 1752 Laptop
B 1121 PC
B 1750 Laptop
C 1321 Laptop
D 1288 Printer
D 1433 Printer
E 1260 PC
E 1434 Printer
E 2112 PC
E 2113 PC

When this answer was written, one query used hd>=10 and one used hd>='10'. The query has since been edited.
When you use the '10' in the comparison, you cause the DBMS to do the comparison as a string instead of as a number. Under string comparison, 9 is greater than 10.
As a general rule of thumb, if the data column is a string type, you should compare it with a string: if the column is numeric type, you should compare it with plain numbers (not with strings). Note that different DBMS may have different ways of interpreting a mixed-type expression.

select distinct product.maker, speed from laptop
join product
on (product.model=laptop.model)
where hd>='10'
or
select distinct product.maker,speed from product,laptop
where product.model=laptop.model and hd>='10'

Select P.maker, L.speed
from Laptop L, Product P
where P.model =L.model
And L.hd > = 10
Group by P.maker, L.speed
Try to use group by statement to get the correct solution.

Select distinct Product.maker,Laptop.speed from Product,Laptop where Product.model=Laptop.model and Laptop.hd>=10 and type='laptop'

select DISTINCT maker, speed
from Product
join Laptop on (Product.model=Laptop.model)
where hd >= 10
order by speed asc

You forgot to filter for the laptops:
SELECT Product.maker, Laptop.speed
FROM Product INNER JOIN Laptop ON Laptop.model = Product.model
WHERE Laptop.hd >= 10 AND Product.type = 'Laptop'

Use keyword Distinct before product.maker.

Related

Visualize all products of the order with the highest value, to extract the largest amount and orders from it

Desired result
NAME PRICE
Lenovo X1 Carbon 4009.00
Lenovo ThinkVision X1 1549.00
Lenovo YOGA 520 1349.00
Motorola Moto Z2 999.00
Motorola Moto Z 549.00
Motorola Moto E5 179.00
ID CATEGORY NAME PRICE ORDER_ID
1 PC Lenovo ThinkaPad L380 1579 5
2 Mobile Motorola Moto E5 179 1
3 Mobile Motorola Moto Z 549 2
4 Monitor Lenovo ThinkVision X1 1549 4
5 PC Lenovo X1 Carbon 4009 3
6 Mobile Motorola Moto Z2 999 4
7 PC Lenovo Legion Y530 2099 4
8 PC Lenovo YOGA 520 1349 3
9 Monitor Lenovo ThinkVision X1 1549 6
10 PC Lenovo YOGA 520 1349 6
11 Monitor Lenovo ThinkVision X1 1549 3
12 Mobile Motorola Moto Z2 999 3
13 Mobile Motorola Moto E5 179 3
14 Mobile Motorola Moto Z 549 3
I used these 2 queries to extract information from the table in the photo
but i need to find a way to retrieve the information with just one query please help
SELECT TOP 1 NAME , PRICE, ORDER_ID
FROM PRODUCTS
ORDER BY PRICE DESC
SELECT NAME ,PRICE
FROM PRODUCTS
WHERE ORDER_ID like '%3%'
ORDER BY PRICE DESC
Desired result
SELECT NAME ,PRICE
FROM PRODUCTS
WHERE ORDER_ID =
(
SELECT TOP 1 ORDER_ID
FROM PRODUCTS
ORDER BY PRICE DESC
)
-- Get everything from the table
SELECT
*
FROM
Products
-- Where the total order value for the order is the highest
Where
order_id =
(
-- The largest order total
SELECT TOP 1
order_id
FROM
Products
GROUP BY
order_id
ORDER BY
SUM(price) DESC
)
ORDER BY
Price DESC

How to Properly Write SELECT Statements When Dealing with Multiple Tables

To demonstrate what I am asking, I will present two tables: The Product table and the PC table. The product table contains the following information:
MAKER MODEL TYPE
----- ---------- -------
A 1232 PC
A 1233 PC
A 1276 Printer
A 1298 Laptop
A 1401 Printer
A 1408 Printer
A 1752 Laptop
B 1121 PC
B 1750 Laptop
C 1321 Laptop
D 1288 Printer
D 1433 Printer
E 1260 PC
E 1434 Printer
E 2111 PC
E 2112 PC
The PC table contains the following information:
CODE MODEL SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ---------- ----------
1 1232 500 64 5 12x 600
2 1121 750 128 14 40x 850
3 1233 500 64 5 12x 600
4 1121 600 128 14 40x 850
5 1121 600 128 8 40x 850
6 1233 750 128 20 50x 950
7 1232 500 32 10 12x 400
8 1232 450 64 8 24x 350
9 1232 450 32 10 24x 350
10 1260 500 32 10 12x 350
11 1233 900 128 40 40x 980
12 1233 800 128 20 50x 970
In order to execute a query that returns all PC models, which of the following queries would be better:
SELECT model FROM pc;
OR
SELECT model FROM product JOIN pc USING(model);
Since you only need PC models, the first query you stated will be OK to use.
In your second query, you are getting the product table content first then joining them into PC table, which is slower than your first query and since you do not need any column from Product table, it is not necessary.
You can go with the query below as an alternative, to return only PC related rows, if you do not add the where clause and there is a PC model as 1276. your second query would return the A - 1276 - Printer row.
SELECT DISTINCT model FROM product JOIN pc USING(model) where product.type='PC';

how to make Numbering rows in a query

I have a simple select statement:
Get numbering of rows from Product table in the following order: a name of the manufacturer in
--decreasing order of quantity of models produced by it (when there is identical quantity of models
--for a number of manufacturers, their names should follow in increasing alphabetic order), model (increasing order).
--Output: number in accordance with the above order, a name of the manufacturer (maker), model
RESULT SHOULD BE -
no maker model
1 A 1232
10 E 2112
11 E 2113
12 B 1121
13 B 1750
14 D 1288
15 D 1433
16 C 1321
2 A 1233
3 A 1276
4 A 1298
5 A 1401
6 A 1408
7 A 1752
8 E 1260
9 E 1434
MY QUERY :
with result as
(select *, count(*) over (partition by maker) as ModelsCount from Product)
select row_number() over (order by ModelsCount desc,model) AS No, Maker, Model
from result
MY RESULT:
no maker model
1 A 1232
10 E 2112
11 E 2113
12 B 1121
13 D 1288
14 D 1433
15 B 1750
16 C 1321
2 A 1233
3 A 1276
4 A 1298
5 A 1401
6 A 1408
7 A 1752
8 E 1260
9 E 1434

Grouping and summarizing values

Find out makers who produce only the models of the same type, and the number of those models exceeds 1.
maker model type
A 1232 PC
A 1233 PC
A 1276 Printer
A 1298 Laptop
A 1401 Printer
A 1408 Printer
A 1752 Laptop
B 1121 PC
B 1750 Laptop
C 1321 Laptop
D 1288 Printer
D 1433 Printer
E 1260 PC
E 1434 Printer
E 2112 PC
E 2113 PC
The result should be
Model Type
D Printer
I tried
Select maker,type from Product
Group by maker ,type
Having count(*)>1
The above query gives me the maker whose number of model exceeds one .But im not able to find the model which only produces same type .Please help
Select Distinct Maker, Type From Product
Where Maker In (Select Maker From Product
Group By Maker
Having Count(Distinct Type) = 1
And Count(Distinct Model) > 1)

Remove duplicates from the below SQL query

SQL Table
id component price manufactured
1 fx card 500 2011
2 ram 400 2010
3 case 400 2010
4 smps 500 2011
5 cord 200 2010
6 usb 200 2010
Expected output (Return components of same price and manufactured yr as distinct combinations):
component component price manufactured
smps fx card 500 2011
case ram 400 2010
cord usb 200 2010
Query tried
SELECT m1.[component]
,m2.[component]
,m1.[price]
,m1.[manufactured]
FROM [dbo].[Mfg] m1
inner join [dbo].[Mfg] m2
on m1.component != m2.component
and m1.price = m2.price
and m1.manufactured = m2.manufactured
Result from above query (wrong output though):
component component price manufactured
smps fx card 500 2011
case ram 400 2010
cord usb 200 2010
ram case 400 2010
fx card smps 500 2011
usb cord 200 2010
Please help me eliminating the duplicate combos using the query.
This works if no pairs of components share the same name:
SELECT m1.[component]
,m2.[component]
,m1.[price]
,m1.[manufactured]
FROM [dbo].[Mfg] m1
JOIN [dbo].[Mfg] m2 ON m1.component > m2.component
AND m1.price = m2.price
AND m1.manufactured = m2.manufactured;
All I changed was to trade the inequality operator != for a greater than operator. This way you get every pair once instead of twice.
To provide for the possibility of identical names:
JOIN [dbo].[Mfg] m2 ON m1.id > m2.id
AND m1.price = m2.price
AND m1.manufactured = m2.manufactured;
I assume id is unique, so it can't go wrong.