Problem in sql query - sql

I have two tables called customer and items.
With single query I want to get all the custid and customername whose custid='Custid1' and itemid whose custid='Custid1'.
How can I build this query?? custid is the primary key of customer table. custid is the foreign key of items table.
I have written this query its not listing all the custid, only listing custid thrice... Here is my query:
create procedure spGetCustomer
as
begin
select a.custid, a.custname,b.itemid
from customer a inner join items b on a.custid = b.custid
WHERE b.custid='custid1'
end

Your question isn't quite clear - what is it you really want??
The JOIN you have will list all customers with items where the customer.custid is custid1. But that doesn't seem to be what you're looking for.....
Could it be you're looking for all customers - with or without items - that have a specific custid?? Try something like this:
SELECT
c.custid, c.custname, i.itemid
FROM
dbo.customer c
LEFT OUTER JOIN
dbo.items i ON c.custid = i.custid
WHERE
c.custid = 'custid1'
If the items.custid is indeed a foreign key into the customer table, there shouldn't be any items around that have custid = custid1 that aren't joined to an existing customer.

If your question actually relates to how you pass the parameter in, your proc should be:
create procedure spGetCustomer(#custid1 varchar(100))
as
begin
select a.custid, a.custname,b.itemid
from customer a inner join items b on a.custid = b.custid
WHERE b.custid=#custid1
end

Related

With SqlServer, where * not exists

I got two tables, Orders with two columns as orderid and customerid, and Customers with two columns as customerid and location.
What I'd like to do is find all the customerid in the table Customers, which are not in Orders. For example, Customers.customerid = {A, B, C, D}, Orders.customerid = {A, B, C}, guess what I need to do is just get the ones from Customers but not exists in Orders. For achieving that, I put,
select customerid from Customers where customerid not exists (select customerid from Orders)
But it returns nothing.. My logic is quite simple like, first got all customerid in table Orders, then get the ones which doesn't exisit in the customerIds from table Orders. I can't see why this is wrong..
I tried this later, and it works. May anyone can help me pls?
select customerid from Customers as c where customerid not exists(select customerid from orders as o where c.customerid = o.customerid)
Why do I have to add c.customerid = o.customerid?
Why do I have to add c.customerid = o.customerid?
Because just because you're using the same name for two columns in your database, that doesn't mean that any specific relationship is enforced or assumed between them.
You need to add the c.customerid = o.customerid to specify that you're interested in the specific condition that these two columns are equal.
But any other correlation condition is also allowed by the language. E.g. you could write a query:
select customerid from Customers as c where not exists(
select customerid from Customers as c2 where c2.customerid < c.customerid)
Which would find you the "first" customer, if considering the customers sorted by their customerid values (not that this is the best way of writing this query, it's just a demonstration of the flexibility)
Your first query was, in effect "give me all rows from the Customer table, provided that no rows exist in the Order table" - which is also a perfectly valid thing to ask for, but wasn't what you intended - you intended to perform some form of correlation, which is what you did in your second query.
May be you need:
select customerid from Customers where customerid not in (select customerid from Orders)
Try below query :
SELECT customerid from Customers C WHERE NOT EXISTS
(
SELECT 1 FROM orders O WHERE C.customerid = O.customerid
)
what you need is
select c.customerid from customer c inner join order o on c.customerid = o.customerid where c.customerid not in (select od.customerid from order od)
you can't access data of 2 tables without joining them.
The syntax is a bit off. You mean to write it like this:
SELECT C.CustomerID
FROM Customers C
WHERE NOT EXISTS
(
SELECT O.CustomerID
FROM Orders O
WHERE O.CustomerID = C.CustomerID
)
;
You could also do this with NOT IN, as such:
SELECT customerid
FROM Customers
WHERE customerid NOT IN
(
SELECT customerid
FROM Orders
)
;
The two are semantically equivalent, for the most part.
Some people will probably tell you that you could do the same thing with a LEFT JOIN / IS NULL construct, but you can look at this article to see why that is a poorer choice in many circumstances.
#Damien_The_Unbeliever gave correct explanation and u need to try like this
with some data i created for 2 tables
CREATE TABLE #Orders
(orderid varchar(10), customerid varchar(10))
insert into #Orders values
('venkat','a'),
('raj','b'),
('mahes','c')
CREATE TABLE #Customers
(customerid varchar(10), [location] varchar(10))
insert into #Customers values
('a','and'),
('b','bar'),
('c','board'),
('D','board1')
SELECT cu.customerid from #Customers CU WHERE NOT EXISTS
(
SELECT 1 FROM #orders b WHERE Cu.customerid = b.customerid
)
output
customerid
D

Select rows that don't have a corresponding join in join table

I have two SQL tables - customer and widget. There's a join table, customers_widgets between them, that has two columns (customer_id and widget_id)
Is there a way I can select all the customers that aren't joined to a widget? So they have an id that doesn't appear in the customer_id column on the join table?
In general I've found NOT IN to be expensive and slow, but your mileage may vary on different RDBMS.
The two alternatives that I most often use are:
SELECT
*
FROM
customer
WHERE
NOT EXISTS (SELECT *
FROM customers_widgets
WHERE customers_widgets.customer_id = customer.customer_id
)
And...
SELECT
customer.*
FROM
customer
LEFT JOIN
customers_widgets
ON customers_widgets.customer_id = customer.customer_id
WHERE
customer_widgets.customer_id IS NULL
Try this:
SELECT customer_id
FROM customer
WHERE customer_id NOT IN (SELECT customer_id
FROM customers_widgets)
You can use an OUTER JOIN for this:
Select C.*
From customer C
Left Join customer_widgets W On C.customer_id = W.customer_id
Where W.customer_id Is Null

Return customers with no sales

I'm a bit of a beginner with SQL so apologies if this seems trivial/basic. I'm struggling to get my head around it...
I am trying to generate results that show all customers that are in the customer table that have never placed an order and will therefore have no entry on the invoice table.
In other words, I want to select all customers from the customer table where there is no entry for their customer number in the invoice table.
Many thanks,
Mike
SELECT *
FROM customer c
WHERE NOT EXISTS (
SELECT 1
FROM invoice i
WHERE i.customerid = c.customerid
)
I would suggest you also read Oracle's documentation on different types of table joins here.
if customer_id is the collumn that identify the customer you should do something like this...
select * from Customer
where customer_id not in (select customer_id from invoice)
If you want to return all customer rows, then you will want to use a LEFT JOIN
select *
from customer c
left join invoices i
on c.customerid = i.customerid
where i.customerid is null
See SQL Fiddle with Demo
If you need help learning JOIN syntax, then here is a great visual explanation of joins.
A LEFT JOIN will return all rows from the customer table even if there is not a matching row in the invoices table. If you wanted to return only the rows that matched in both tables, then you would use an INNER JOIN. By adding the where i.customerid is null to the query it will return only those rows with no match in invoices.

Create a stored procedure to delete rows that have no values

Create a stored procedure called sp_del_inactive_cust to delete customers that have no orders. The stored procedure should delete 1 row.
Here is the database I am working with.
(source: bcitwebdev.com)
My immediate thought is that I need to check the customers table against the orders table. If the customer is present in the customers table but not in the orders table, that must mean that they have not created any order ids under their customer id. Then, I must delete the customer if they do not have any orders.
I'm not really sure how to go about scripting this problem. I need help! Please keep it simple as I am a first semester student.
Here's something I've tried starting:
CREATE PROCEDURE sp_del_inactive_cust
AS
SELECT customers.customer_id,
orders.customer_id
FROM customers
INNER JOIN orders ON customers.customer_id=orders.customer_id
WHERE /* customer_id is found in customers table but not in orders table */
And then I will execute the procedure.
This question has been answered thanks to the help of Michael Fredrickson.
Here are the final statements which deleted the required 1 row:
CREATE PROCEDURE sp_del_inactive_cust
AS
DELETE customers
FROM customers
LEFT OUTER JOIN orders ON customers.customer_id = orders.customer_id
WHERE orders.customer_id IS NULL;
GO
EXECUTE sp_del_inactive_cust;
GO
CREATE PROCEDURE sp_del_inactive_cust
AS
DELETE TOP (1) c
FROM
customers c
LEFT OUTER JOIN orders o
ON C.customer_id = o.customer_id
WHERE
o.customer_id IS NULL
You will want to create an anti-join to get the customers that have placed no orders, like this:
DELETE c FROM
FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL
I hate to just give you the answer so I will give you an example of the inverse and let you figure out the rest
here is an example
2 tables
foo {foo_id, a, b, c}
bar {bar_id, foo_Id, e, f, g}
If i want to delete all records from foo that appear in the bar table I would write
delete foo
where foo_id in (select foo_id from bar)
look up the "in" keyword for a real explanation
also there are always many ways of doing something and you might not get the proper grade in a class if you dont do things the way the teacher wants
DELETE customers
WHERE customer_id NOT IN (SELECT customer_id FROM Orders)

Deleting records that are not joined on in SQL

I have a SQL Server 2008 database. This database has two related tables: Customer and Order. My tables look like this:
Customer
--------
ID
FirstName
LastName
Order
-----
ID
CustomerID
Amount
ShipDate
I need to delete all customers that do NOT have any orders. I cannot figure out the best way to do this. Can someone tell me how to do this? the NOT part is what keeps getting me. Originally I was using "IN" but it's stumping me.
Thank you for your help!
Rather than using the IN operator, use a subquery and NOT EXISTS, something like:
DELETE Customers
FROM Customers c
WHERE NOT EXISTS (
SELECT 1
FROM Orders o
WHERE o.CustomerID = c.ID
)
DELETE Customer
WHERE ID IN
(
SELECT Customer.ID
FROM Customer
LEFT JOIN Order ON
Customer.ID = ORder.CustomerID
WHERE ORder.CustomerID IS NULL
)
Delete from Customer
WHERE ID in
(
SELECT C.ID
FROM Customer C
Left Outer Join Order O ON C.ID = O.CustomerId
WHERE O.ID IS NULL
)