SQL - all join statement? - sql

I am learning SQL and have been tasked with creating a query for this:
-write a select all join statement on userDb.user and userDb.advertiser based on network_id where user_name = 'finance'
However, I cant find anything that suggests an all joint statement so I am confused on what to do?
My attempt so far however, does not work!
SELECT userDB.user.network_id,
userDB.advertiser.network_id
FROM userDB.user
INNER JOIN userDB.advertiser ON userDB.user.network_id = userDB.advertiser.network_id
WHERE network_id = user_name = 'finance'
What is the correct code and breakdown explanation?

Ok, the query you're looking for is something like this;
SELECT *
FROM user AS u
INNER JOIN advertiser AS a
ON u.network_id = a.network_id
WHERE u.user_name = 'finance'
You were almost there. The changes I've made is to use table alias' ('u' and 'a') which it's worth reading up on. Your where clause also had too many operators in there.
The SELECT * is what I guess they mean by SELECT ALL
Let me know if you don't understand any of the query above and I'll be happy to help.

Related

Creating a view with a 'WHERE NOT IN' SQL subquery

I'm trying to turn the following SQL query into a view.
SELECT * FROM system_accounts
WHERE system_accounts.id
NOT IN (SELECT account_id
FROM system_group_members
WHERE system_group_members.group_id = 1);
In other words, how do I go about creating a view that gives me all the non-members of a certain group, with those non-members coming from the total set of accounts known to the system?
The query works fine when I test it in my Adminer window. However, I'm at a loss how to express the variable group_id (which in my example is '1') in correct SQL for a view.
I'm sure I'm missing something trivial, but this is the sort of thing I'll bang my head on for hours. Hopefully some kind soul here will help me out.
Many thanks for your time.
You can create a view that has all non-members of all groups. Then you can filter down to the group you want:
CREATE VIEW v_nonmembers AS
SELECT g.group_id, sa.*
FROM system_groups g cross join
system_accounts sa LEFT JOIN
system_group_members sgm
ON gsm.group_id = g.group_id AND
gsm.account_id = sa.id
WHERE gsm.group_id IS NULL;
You can then use it as:
SELECT *
FROM v_nonmembers
WHERE group_id = 1;

Execute SQL query based on results of a condition

My SQL knowledge is somewhat limited. I have 2 Select statements that returns separate sets of results. Since the tables being queried are different sets of tables for each Select statement I would like to know if it is possible to write it such that it picks which query to execute based on the result returned from a Select Statement on a completely different table, for example:
Select C.ManBtchNum from Table C
If OITM.ManBtchNum = 'Y' then
Select * from Table A
else
Select * from Table B
Apologies for using psuedocode but its the shortest way I can explain this.
I'm not sure if a Case expression can be used here. Any advice would be helpful. Thanks
I have tried both a Case as well as a Union but I'm not getting the results needed. Granted I might be doing something wrong considering my limited knowledge
As an example,
select t1.Val, t2.Val, t3.Val,
case
when(t1.Val = Something)Then (T2.Val)Else(t3.Val)
END
from tablex t1
inner join tabley t2 on t1.commonfield = t2.commonfield
inner join tablez t3 ON t1.commonfield = t3.commonfield

Issue with joins in a SQL query

SELECT
c.ConfigurationID AS RealflowID, c.companyname,
c.companyphone, c.ContactEmail, COUNT(k.caseid)
FROM
dbo.Configuration c
INNER JOIN
dbo.cases k ON k.SiteID = c.ConfigurationId
WHERE
EXISTS (SELECT * FROM dbo.RepairEstimates
WHERE caseid = k.caseid)
AND c.AccountStatus = 'Active'
AND c.domainid = 46
GROUP BY
c.configurationid,c.companyname, c.companyphone, c.ContactEmail
I have this query - I am using the configuration table to get the siteid of the cases in the cases table. And if the case exists in the repair estimates table pull the company details listed and get a count of how many cases are in the repair estimator table for that siteid.
I hope that is clear enough of a description.
But the issue here is the count is not correct with the data that is being pulled. Is there something I could do differently? Different join? Remove the exists add another join? I am not sure I have tried many different things.
Realized I was using the wrong table. The query was correct.

I create view to show only specific city_name student fee but it give me student name with all fee

create or replace view v_amount
as
select s.s_name, s.s_fname, b.fee
from student s, amount b
where s.s_add = 'Peshawar'
order by s.s_name;
Do you need to explicit a relationship 1/1
Do you have some field with join S+B? look in the example for key fields
Create or replace view v_amount as select s.s_name,s.s_fname,b.fee
from student s, amount b where s.s_add= 'Peshawar' AND s.key=b.key order by s.s_name;
If you don't understood post your CREATE for each table and all fields
You don't have a join clause so you are getting a CROSS JOIN.
You need something like
WHERE s.key = b.related_key
Using the syntax you are using.
Not sure what database you are using, but it is always best to use an ANSI join if it is supported. Then you would get an error that made it clear what the problem is. So if it is supported your query would be better written like this:
SELECT s.s_name, s.s_fname, b.fee
FROM student s
INNER JOIN amount b
ON s.key = b.related_key --you missed out this bit
WHERE s.s_add = 'Peshawar'
ORDER BY s.s_name;
If it were written this way you would have got an error message telling the ON clause was missing when you ran it.

Dynamic sql join in sql server

I have a relationship between two tables. The two tables PKs are int types.
In one table (UserS), I need to supply the Username and get the corresponding ID (which is the PK). This is the standard ASP.NET user table when using forms authentication.
However, in a related table, I want to supply the ID I find from the Users table to get a value out.
Something like:
Run query to get ID for a username (simple select/where query)
Return the result
Run a subquery where I can pass in the above result -
Get value where ID = result
This sounds a lot like dynamic sql. However, there might be a better suited and appropriate way of writing this query (on Sql Server 2k5).
How can I go about doing this and what gotchas lurk?
EDIT: I will try something along the lines of http://www.sqlteam.com/article/introduction-to-dynamic-sql-part-1
EDIT: Thanks for the tips everyone, I wrote this:
SELECT Discount.DiscountAmount
FROM Discount
INNER JOIN aspnet_Users
ON Discount.DiscountCode = aspnet_Users.UserId And aspnet_Users.Username = 's'
Where 's' is to be replaced by a parameter.
Thanks
You don't have to use dynamic SQL for that.
You can use a lookup instead:
DECLARE #ID bigint
SELECT #ID = ID FROM Users WHERE Username = #Username
SELECT
*
FROM
TableB
WHERE
ID = #ID
Then, you could add the PARAMETER #Username to your SqlCommand object, preventing the risks of SQL Injection.
Also, doing the lookup is preferable to a join, since the index is scanned a single time, for TableB.
Right, i just would do this:
SELECT *
FROM TableB
JOIN Users ON Users.Id = TableB.ID
WHERE Users.Username = #Username
Regarding lookup vs joins - while it may seem more intuitive for the novice to use the lookup, you should go with a join. A lookup needs to be evaluated for every row in your primary result set, while a join is evaluated once. Joins are much more efficient, which means that they are faster.
This is just a simple join isn't it?
SELECT x.*
FROM user_table u
INNER JOIN
other_table x
ON u.id = x.user_id
WHERE u.name = #user_name
SELECT values.value
FROM form_users, values
WHERE form_users.username = 'John Doe'
AND values.user = form_users.userid
SELECT
*
FROM
table2 t2
JOIN
UserS t1 ON t2.IDKey = t1.IDKey
WHERE
UserS.Username = #Input