Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
so I have 3 tables
table 1: team
| team_id | name |
-----------------------
| 1 | alpha |
| 2 | beta |
| 3 | gamma |
table 2: buildings
| building_id | name |
---------------------------
| 1 | Baxter |
| 2 | LexCorp |
table 3: team location
| team_id | building_id |
-------------------------
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
What I need now is an sql query that will list the names of the teams located in the baxter building and I cant for the life of me even think where to begin on this one, im quite new to SQL
try this one
SELECT team.name FROM team_location
INNER JOIN buildings ON buildings.building_id = team_location.building_id
INNER JOIN team ON team.team_id = team_location.team_id
WHERE buildings.name = 'Baxter'
SELECT t.NAME
FROM team as t
INNER JOIN teamLocation AS tl ON t.team_id=tl.team_id
INNER JOIN buildings As b ON tl.building_id=b.building_id
WHERE b.name='Baxter'
Please check this sql fiddle
http://sqlfiddle.com/#!6/45e2e/1
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I have next problem.
There is a table with four columns:
|ID | X | Y | VAL |
|:--|:--:|:--:|----:|
|1 | 1 | 1 | 1110|
|2 | 1 | 2 | 1720|
|3 | 1 | 3 | 2330|
|4 | 1 | 4 | 2940|
|5 | 1 | 5 | 3550|
...
When user enter some value in text field e.g. 2370 i need function to find is there exact match in VAL field and if not to find very first bigger than 2370 (2940) and return ID value.
In some other language I can do it trough dictionaries and so one but in VBA I simply don't have idea.
Any idea or help will be appreciated.
You can use a query to get this answer, using TOP 1 to just return 1 record:
SELECT TOP 1 tblData.ID, tblData.VAL
FROM tblData
WHERE (((tblData.VAL)>=2370))
ORDER BY tblData.VAL ASC;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i have two tables. users and accounts.
accounts table :-
id | accountName | userId
--------------------
1 | natt | 1
2 | kelly | 2
3 | john | 3
users table :-
id | username |
--------------------
1 | natt#xyz.com |
2 | kelly#xyz.com |
3 | john#xyz.com |
4 | randy#xyz.com |
5 | jamie#xyz.com |
expected output: -
userId |
---------
4 |
5 |
as you can see id of users table act as foreign key in accounts table.
I want to fetch every user that does not have a account associated with it(4,5 in my example). i can do it via IN or NOT IN but thats not the fastest way. can some tell me the fastes way to do this? im using postgres.
SELECT T.ID,T.USERNAME
FROM USERS T
WHERE NOT EXISTS
(
SELECT 1 FROM ACCOUNTS A WHERE A.USERID=T.ID
)
You can try NOT EXISTS
SELECT U.userId FROM users U
LEFT OUTER JOIN accounts A ON U.id = A.userId
WHERE A.id IS NULL
I've got 2 tables, a questions table and an answers table with the following example data:
+-----------------------------------+
| Questions |
+----+------------------------------+
| id | title |
+----+------------------------------+
| 1 | What is your favourite game? |
| 2 | What is your favourite food? |
+----+------------------------------+
+-------------------------------------------------+
| Answers |
+----+------------------------------+-------------+
| id | text | question_id |
+----+------------------------------+-------------+
| 1 | The Last Of Us | 1 |
| 2 | PlayerUnknowns Battlegrounds | 1 |
| 3 | Uncharted | 1 |
| 4 | KFC | 2 |
| 5 | Pizza | 2 |
+----+------------------------------+-------------+
Creating a one to many relationship as in one question can have many answers, I can do any of the following:
SELECT
id, text
FROM
answers
WHERE
question_id = 1
Or:
SELECT
answers.id, answers.text
FROM
answers
JOIN
questions
ON
answers.question_id = questions.id
WHERE
questions.id = 1
Or:
SELECT
answers.id, answers.text
FROM
questions
JOIN
answers
ON
questions.id = answers.question_id
WHERE
questions.id = 1
Which all return the following (expected) results:
+-----------------------------------+
| Results |
+----+------------------------------+
| id | text |
+----+------------------------------+
| 1 | The Last Of Us |
| 2 | PlayerUnknowns Battlegrounds |
| 3 | Uncharted |
+----+------------------------------+
Should any of them be avoided? Is there a preferred way of doing this? Just curious about the dos and don’ts of querying relationships in general really.
If you only want to get the answers, don't involve the questions table.
Just select from the answers.
Adding unused tables into your query makes no sense at all -
It makes the query harder to read, thus harder to maintain,
and It makes the database work harder (though modern databases might just optimize the unused parts of the query away) to get the same results.
If you want to imply relationship between "questions" and "answers" table then you can make id column from "questions" table as Primary key and
question_id column from "answers" as Foreign key
and you use JOIN when you need data(columns) from more than one table
in your case if you want title column to be included then you can JOIN tables
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table like this
MAIN ID CONTENT SUB ID
ABCD ONE 888
ABCD TWO 888
which i would like the query result to be like this
MAIN ID SUB ID CONTENT1 CONTENT2
ABCD 888 ONE TWO
You can use the PIVOT function:
select
*
from (
select
[main id],
[sub id],
[content],
'content' + cast(
row_number() over (partition by [main id],[sub id] order by content)
as varchar(5)) as contentIX
from
table1
) T
pivot (max(Content) for contentIX in (content1,content2)) as content
The subquery first generates a field name for each result to pivot, content1, content2, etc. that looks like this:
| MAIN ID | SUB ID | CONTENT | CONTENTIX |
|---------|--------|---------|-----------|
| ABCD | 888 | ONE | content1 |
| ABCD | 888 | TWO | content2 |
Then the outer query performs a pivot over the CONTENTIX column to get the final result:
| MAIN ID | SUB ID | CONTENT1 | CONTENT2 |
|---------|--------|----------|----------|
| ABCD | 888 | ONE | TWO |
Demo: http://www.sqlfiddle.com/#!6/095bb/11
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have three tables:
IDcontacts | FirstName | LastName
----------------------------------
1 | Walt | Ne
IDcompany | CompanyName
------------------------
1 | Universe
IDowner | IDcontacts | IDcompany
---------------------------------
1 | 1 | NULL
2 | NULL | 1
3 | NULL | NULL
I need a query that will give me the following output:
IDoutput | Name
--------------------
1 | Walt Ne
2 | Universe
3 | NULL
To do a join and get half a result even if the other record does not exist, use an outer join.
To chose the first non-NULL value of a list, use COALESCE:
SELECT IDowner as IDoutput,
COALESCE(FirstName || ' ' || LastName, CompanyName) AS Name
FROM Owner
LEFT JOIN Contacts ON Owner.IDcontacts = Contacts.IDcontacts
LEFT JOIN Company ON Owner.IDcompany = Company .IDcompany