SQL: ORDER BY with 2 criteria not working - sql

In my work I have 2 tables:
Airports(serial_number, name, maker, model_name (which is a foreign key to the next table), op_start_date, op_end date
Models (name, number_rows, is_narrow, is_wide)
With them I have to make a table with the information for each airplane of their name, maker, model, and if they are wide-body or narrow-body. Adiotionally, I am supposed to order the list by their maker and their model.
The code I tried is below
SELECT maker, Airplanes.name, model_name, is_narrow, is_wide
FROM Airplanes
JOIN Models ON model_name=Models.name
ORDER BY maker, model_name;
However the table obtained was this one:
Link to the image:
As you can see, it's ordered by maker, but not model. Can anyone help me with this problem?

This could occur if you had invisible characters in the maker.
Try running:
SELECT a.maker, count(*)
FROM Airplanes a
GROUP BY a.maker
ORDER BY a.maker;
See if the "same" value for maker turns up more than once.

Related

SQL - I cant think of any way to do this, i have done all the research?

This is my cars table which has 1000 rows. Each of make or manufacturer has different model and same model can have different price.
I have to output min price among all of the model of each manufacturer and i cant think of any way to do this.
Nearest to what i got is,
SELECT make, model, min(price) FROM car
GROUP BY model, make
ORDER BY make;
which outputs,527 rows
But i want min price among all of the models of each make.
HELP!!
In Postgres, I recommend distinct on for this purpose:
select distinct on (make) c.*
from cars c
order by make, price asc;
From your question it seems that you want minimum price of each make.
The following query can help in my opinion.
SELECT make,MIN(price) from car
group by make
order by make;
Assuming you need "the lowest priced Model for each Make", you can easily get the lowest price per Make according to the answers given, but that will not give you the Model name!
I'd suggest using a window function to rank the data, partitioning by Make, and ordering by Price ascending. Then simply select all rows ranked 1.
There may be a possibility that two Models within a Make may be equally low priced. In that case, you'd have two rows returned for that Make. If that's a possibility and also a problem, you'd have to engage in further processing to decide how to break the tie or consolidate the row into one (for example, by concatenating the Model names).
Use rank() window function inside a CTE to filter the minimum prices:
with cte as (
select *,
rank() over (partition by model order by price) rn
from car
)
select id, make, model, price
from cte
where rn = 1
order by make;
This will return ties in minimum price.
If you don't need ties replace rank() with row_number().
I assumed that a model name cannot be used by 2 makers. If this is not the case then change to this:
rank() over (partition by make, model order by price) rn
I think you are looking for a simple left join.
select t1.make, t1.model, t2.min_price
from car t1 left join (select make, min(price) min_price from car group by make) t2 on
t1.make=t2.make

Microsoft Access SQL Homework Issue

For each maker that produces printers, find the number of printer models produced by this maker. Output a table consisting of two columns: (maker, number of models). Sort the result by numbers of models in ascending order.
What I have tried so far:
SELECT maker, Count(*) AS [Number of Models]
FROM (SELECT DISTINCT model
FROM Product
WHERE type='printer')
It doesn't work, prompts and asks me to enter a maker instead of inserting makers from my list.
You can simply use this query :
SELECT maker, Count(distinct model) AS [Number of Models]
FROM Product WHERE type='printer'
group by maker
order by Count(distinct model)

Displaying count of duplicates and removing duplicates at the same time?

Apologies if the title doesn't make full sense, I'll try to explain as best I can.
I have a table containing information about vehicles, there are many duplicates and around 5000 rows overall. Here's a snippet as an example:
As you can see the model '159 TI TBI' repeats twice, this essentially means there are two of these cars stored in London.
I am looking for something like below, where there is a count of how many times a particular vehicle in a particular location repeats, as well as removing duplicates so each vehicle only appears once for each location.
I am able to do a fairly simple select command for a particular vehicle and location, such as
SELECT COUNT(model), model, loc_name, vehicle_type
FROM vehicles
WHERE loc_name='London' AND model='159 TI TBI'
GROUP BY model, loc_name, vehicle_type
The issue is that I'd be repeating this command for every combination of a vehicle model in a particular location, it's not very efficient.
Hopefully this makes some sense, I haven't had a huge amount of experience with SQL so apologies if anything is badly wrong. Thanks.
This query will give you the required results
SELECT COUNT(model) cnt, model, loc_name, vehicle_type
FROM vehicles
GROUP BY model, loc_name, vehicle_type
Your question is a bit unclear. But let me try. It seems you think to get the count for each group, you would have to re-query with for each vehicle in the where clause. However, aggregation will allow you to get the count across all the vehicles. If you are just looking for the model, location, type uniquely and the count of occurrences, you have the right query, just remove your where clause and the power of SQL will take care of it for you.
SELECT COUNT(*) as quantity, model, loc_name, vehicle_type
FROM vehicles
GROUP BY model, loc_name, vehicle_type
if you want only the rows with more then one occurrence you can use having for filter th aggregated result
SELECT COUNT(*) as quantity, model, loc_name, vehicle_type
FROM vehicles
GROUP BY model, loc_name, vehicle_type
having count(*) > 1

Correlated Sub queries using MAX and GROUP BY

I have a database with the tables: VEHICLES (a Blue Honda Civic costing $30k), BRAND (Honda) AND MODEL (Civic).
I am trying to create a correlated sub query which will give me the VEH_IDs for the cars with the highest price from each BRAND.
Simply, I want to know for all the brands which car costs the most.
I've attached a picture with the SQL im trying but it's not working. Most likely because I have no idea what im doing.
SELECT
BRAND.BRAND_ID, BRAND_NAME, VEHICLE.MODEL_ID, VEH_ID
FROM BRAND, MODEL, VEHICLE
WHERE VEH_PRICE =
(
SELECT MAX(VEH_PRICE)
FROM VEHICLE
)
GROUP BY BRAND.BRAND_ID, BRAND_NAME, VEHICLE.MODEL_ID, VEH_ID;
create a new query on the vehicles table grouping by brandID to determine the max(price)
SELECT brandID, MAX(VEH_PRICE)
FROM VEHICLE
group by brandID
then create another query that uses the first one joined back to vehicles to determine the related vehicleID
SELECT
v.BRAND_ID, MODEL_ID, VEH_ID
FROM VEHICLE v inner join
( SELECT brandID, MAX(VEH_PRICE) as max_veh_price
FROM VEHICLE
group by brandID) m on
v.brandid = m.brandid and
v.veh_price = m.max_veh_price
then, to get the brand_name, join again to your brand table on the brandID field, and yes, if max(price) returns more than one vehicle, you'll have to go with Top 1 at Roman suggests.
sorry, couldn't read your images at first.
yes, you need to join the model table to vehicle to get to the brandID. I'm assuming this is an exercise and you're supposed to learn about joins as a result? don't just take a solution, then, understand each piece individually.

Getting MIN Price in Subquery in SQL Server (using DISTINCT)?

I am trying to get a Minimun price from a car in a table i have.. I am using DISTINCT
SELECT DISTINCT
datepart(year,[Registration]) AS YearRegistered,
MIN(SalePrice), Model, Make
FROM [VehicleSales]
But its not working, for example
without distinct returns many car makes and models so i use distinct so i get unique cars that are the same make and model and year....
I wish to incorporate a "Startign from price ..." hence the SalePrice can also be different for same model and make ... so i want to do a MIN..
But i am bit confused, the above is working working...
Any ideas?
You need to add a GROUP BY clause and get rid of the DISTINCT:
SELECT
datepart(year,[Registration]) AS YearRegistered,
MIN(SalePrice), Model, Make
FROM
[VehicleSales]
GROUP BY
datepart(year,[Registration]), Model, Make
SELECT DATEPART(year,[Registration]) AS YearRegistered, Model, Make, MIN(SalePrice)
FROM [VehicleSales]
GROUP BY
DATEPART(year,[Registration]) AS YearRegistered, Model, Make