I'm trying to select customer names with the same zip code but I can't seem to get the correct result. The correct rows that are meant to be returned are 11. I seem to be getting 14 using the following:
SELECT a.customer_first_name, a.customer_last_name, a.customer_zip FROM customers a
UNION
SELECT b.customer_first_name, b.customer_last_name, b.customer_zip FROM customers b
From here I'm kinda stuck. I know that both statements are the same but this is the basis of where I got to.
I was going to use something along the lines of:
WHERE a.customer_zip=b.customer_zip
But of course this doesn't work and is kind of irrelevant.
Customers table:
Any help would be much appreciated. If I've missed anything and/or this comes across unclear, then I apologise. Thanks.
Edit: The correct result should be 11 rows (which I can't seem to achieve).
I would do:
select customer_zip
from customers
group by customer_zip
having count(customer_zip)>1
edit:
this will give a list of duplicate ZIPs. based on it, you can esily find the customers with those zips with a select * from customers where customer_zip in (previous_query)
SELECT customer_first_name, customer_last_name, customer_zip
FROM customers where customer_zip in
(SELECT customer_zip FROM customers
GROUP BY customer_zip
HAVING COUNT(*) > 1);
Related
I'm new to SQL and recently saw this question, which is described on the [attached picture]
. Any suggestions on how to solve it? Thanks in advance.
In most cases such requirements are better to resolve by starting from the end. Let's try that: "have more than 3 items ... and total price".
select InvoiceId, sum(price)
from InvoiceItem
group by InvoiceId
having count(1) > 3;
This query gives you the overall price for every invoice that has more than 3 items. You might be wondering why there is that funny "having" clause and not the "where" clause.
The database executes first "from" and "where" parts of the query to get the data and only after that using aggregations (sum, count etc.) is possible, so they are to be specified afterwards.
The query above actually returns all the data from requirement, but I assume that whoever gave you this task (that was a teacher, right?) was asking you to retrieve all the data from Invoices table that correspond to the records within the InvoiceItem table that has more than 3 items and so on.
So, for now all you have left is to join the Invoice table with a query from above
select i.id, i.customername, i.issuedate, sum(it.price)
from InvoiceItem it
join Invoice i on it.invoiceid = i.id
group by i.id, i.customername, i.issuedate
having count(1) > 3;
Teacher might ask you why did you use count(1) and not count(*) or count(id) or something. Just tell him/her all these things are equal so you picked just one.
Now it should be working fine presumably. I did not tested it at all as you did not provide the data to test it on and it is late so I am pretty tired. Because of this you might needed to fix some typos or syntax errors I probably made - but hey let's keep in mind you still have to do something on your own with this task.
My Database is like this
CUSTOMER (NAME,VAT,PHONE)
CAR(PLATENR,COLOUR)
RENTS(VAT,PLATENR, RENTDATE)
What I want to find is all the black cars that were rented by all customers (all VAT Numbers).
I want to use nested subqueries and EXCEPT or NOT EXISTS.
I already have this query using COUNT that works.
SELECT rents.PlateNr
FROM Rents,Car
WHERE Car.colour='black' AND car.PlateNr=rents.PlateNr
GROUP BY rents.PlateNr
HAVING COUNT(DISTINCT VAT) = (SELECT COUNT(*) FROM Customer);
I am trying to use this guide http://www.inf.usi.ch/faculty/soule/teaching/2016-fall/db/division.pdf to get my result but I don't get how the query is implemented in my case.
I was looking an answer similar to the link that I provided above regarding division.
After some search and try the final division query using except is this:
SELECT PlateNr
FROM Car
WHERE colour='black'
EXCEPT
SELECT PlateNr
FROM (SELECT car.PlateNr, rents.VAT
FROM car,rents
EXCEPT
SELECT PlateNr,VAT
FROM rents)
Have tried it in POSTGRE that supports EXCEPT and everything works like a charm.
I have two tables. The first one is for sales (name's table is 'ventas') and the other one, for detailed articles by sale (the name is 'ventaArticulos'). Basically, the last one contains all the articles that were sold.
Those are related by the columns ventas.id_venta and ventaArticulos.id_ventaArticulo
Basically, the idea is to make an SQL SELECT for the first table (ventas) for example, getting the columns 'fecha' and 'importe' but also, perform a 'count' with the total of registers that are in the second table related by sale. (ventas.id_venta and ventaArticulos.id_ventaArticulo)
hope to be clear enough and can help me!
SQL to try to clarify (Obviously it doesn't work):
SELECT ventas.fecha, ventas.importe, count(ventaArticulos.id_codigoArt)
FROM ventas JOIN
ventaArticulos
ON ventaArticulos.id_ventaArticulo = ventas.id_venta
Thanks!
I would recommend to use table alise that could be easier to follow & you forgot to include GROUP BY Clause
SELECT v.fecha, v.importe, count(va.id_codigoArt) counts
FROM ventas v -- Use alise v entire the query instead of table_name
INNER JOIN ventaArticulos va ON va.id_ventaArticulo = v.id_venta
GROUP BY v.fecha, v.importe;
SELECT v1.fecha, v1.importe, count(v2.id_codigoArt)
FROM ventas v1 , ventaArticulos v2
where v1.id_ventaArticulo= v2.id_venta
group by v1.fecha, v1.importe
having count(*) > 1
Couldn't find anything on this, using set-based operations.
Not sure if I'm on the right track, but I have 2 guesses on how to do this, but I'm not sure if I'm even close, or if MINUS is the right set-based operation to use:
SELECT customerid
FROM customer
MINUS
SELECT orderid
FROM custorder
WHERE custorder IS NULL;
or
SELECT customerid
FROM customer
MINUS
SELECT orderid
FROM custorder;
Suggestions on how to improve or what to change? Any help is appreciated.
Could someone also possibly explain when to use UNION, UNION ALL, or INTERSECT on a similar sample like this?
Also, I'm not sure if I need these in 2 different tables or not. In this database, 2 of these tables are CUSTOMER (has customerid) and CUSTORDER (has customerid and orderid). Here's a screenshot of the table values if you wanted to see: https://imgur.com/a/vHMC4
NOTE: I HAVE TO USE SET-BASED OPERATIONS ONLY FOR FULL CREDIT
probably more like
select customerid
from customer
MINUS
select customerid
from custorder;
you need to MINUS off the same type of values from the set
I would use not exists:
select c.*
from customer c
where not exists (select 1
from custorder co
where co.customerid = c.customerid
);
You can do something similar with not in and left join/where.
The set-based approach is:
SELECT customerid
FROM customer
MINUS
SELECT customerid
FROM custorder;
The advantage of the first method is that you can select additional columns from customer.
I want to select the entire first row of each record where a promo code is unique. I am trying to create a samples table, in this table will be one record (the first record) from each distinct promo code. I have asked all of my co-workers and they usually go though the data by hand and select one from each. the problem is that the number of promo codes grows each time and the codes change. so I want to write a query that will select the first record found to have each distinct code. so for I have something like this:
SELECT DISTINCT Customer.promo1 FROM Customer AS promo;
SELECT * FROM Customer, promo
WHERE Customer.promo1 = promo.promo1;
But this obviously give the original table. I do have a ID field called AutoID in Customer.
Thanks in advance.
I'm assuming you want the first Customer.AutoId associated with each Customer.Promo
SELECT
c.*
FROM
Customer c
INNER JOIN
(
SELECT
c.promo1,
MIN(c.AutoID) AutoID
FROM
Customer c
GROUP BY
c.promo1) FirstCusomterWithPromo
ON c.AutoID = FirstCusomterWithPromo.AutoID
Something like that:
SELECT * FROM Customer
GROUP BY Customer.promo1