self join multiple accounts same id - self-join

I am still learning how to use self-joins.
I would like to find out if somebody has multiple accounts by querying the same id.
My understanding of self join is the following:
select,
a.account_number,
a.person_id,
b.person_id
from table a
inner join table b
on a.person_id = b.person_id
But it is bringing back the person_id twice but not showing me if there are different accounts.
Can I have some guidance please?
Many Thanks,
JB

Related

Cant navigate into a specific column sqlite

The first link is the problems.
I am very inexperienced in SQLite and needed some help.
Thanks in advance!
https://imgur.com/v7BdVe3
These are all the tables displayed open
https://imgur.com/a/VMOxAuc
https://imgur.com/a/LrDcCBQ
The schema
https://imgur.com/a/bv5KTHN
This is as far as I could get which is close but I couldn't figure out how to sort it also by marina 1.
SELECT BOAT_NAME, OWNER.OWNER_NUM, LAST_NAME, FIRST_NAME from OWNER inner join MARINA_SLIP on OWNER.OWNER_NUM = MARINA_SLIP.OWNER_NUM;
If you know anything else bout the other questions feel free to help me with those too, Thanks!
I believe that you want
SELECT BOAT_NAME, OWNER.OWNER_NUM, LAST_NAME, FIRST_NAME
FROM OWNER INNER JOIN MARINA_SLIP ON OWNER.OWNER_NUM = MARINA_SLIP.OWNER_NUM
WHERE MARINA_NUM = 1
ORDER BY BOAT_NAME;
The second question involves multiple joins.
The third question asks you to use the count(*) function, noting that this is an aggregate function and will result in the number of rows for the GROUP as per the GROUP BY clause (if no GROUP BY clause then there is just the one GROUP i.e. all resultant rows).
The fourth question progresses a little further asking you to extend the GROUP BY clause with the HAVING clause (see link above for GROUP BY).

Using querys to get information from different tables

I am learning SQLite and I'm using this database to learn how to correctly use querys but I'm struggling specially when I have to use data from multiple tables to get some information.
For example, with the given database, is there a way to get the first name, last name and the name of songs that every customer has bought?
All you have to do is a simple SQL SELECT query. When you say you're having trouble getting it from multiple tables, I'm not sure if you're trying to get the data from all of the tables in one single query, as that is not necessary. You just need to have multiple instances of the SELECT query, just for different tables (and different column names).
SELECT firstName, lastName, songName FROM table_name
You have to study about JOINS:
select
c.FirstName,
c.LastName,
t.Name
from invoice_items ii
inner join tracks t on t.trackid = ii.trackid
inner join invoices i on i.invoiceid = ii.invoiceid
inner join customers c on c.customerid = i.customerid
In this query there are 4 tables involved and the diagram in the link you posted, shows exactly their relationships.
So you start from the table invoice_items where you find the bought songs and join the other 3 tables by providing the columns on which the join will be set.
One more useful thing to remember: aliases for tables (like c for customers) and if needed for columns also.
You need to use joins to get data from multiple tables. In this case I'd recommend you using inner joins.
In case your are not familiar with joins, this is a very good article that explains the different types of joins supported in SQLite.
SQLite INNER JOINS return all rows from multiple tables where the join
condition is met.
This query will return the first and last name of customers, and the tracks they purchased.
select customers.FirstName,
customers.LastName,
tracks.name as PurchasedTracks from invoice_items
inner join invoices on invoices.InvoiceId = invoice_items.InvoiceId
inner join customers on invoices.CustomerId = customers.CustomerId
inner join tracks on invoice_items.TrackId = tracks.TrackId
order by customers.LastName

Sqlite Join confusion

My first ever Stackoverflow question so go easy on me, I am not very experienced with SQLite.
I have a table of football teams
CREATE TABLE IF NOT EXISTS teams (
teamId INTEGER PRIMARY KEY NOT NULL,
name TEXT,
);
and a table of matches
CREATE TABLE IF NOT EXISTS matches (
matchId INTEGER PRIMARY KEY NOT NULL,
homeTeamId INTEGER,
awayTeamId INTEGER,
);
I am trying to work out the SELECT statement that would display the list of matches but would replace both the homeTeamId and the awayTeamId numbers with the team names.
I have tried several variants but because two fields in the matches table join back to the same teams table I am getting either syntax errors or ambiguous column errors.
Sorry for a fairly basic question, any help appreciated.
Geoff
Try this:
select m.matchId, h.name, a.name
from matches m
inner join teams h on h.teamId = m.homeTeamId
inner join teams a on a.teamId = m.awayTeamId
You have not posted the variants you have tried, so I just assume that you the problem was caused by not using aliases (in my example h and a) for the table teams. You have to use at least one alias, if you need to join one table twice.
Thanks for the speedy reply. In the typical way I stumbled on the solution about 15 minutes after I posted my question, my solution was identical to yours except that I didn't bother aliasing the matches table.
Your answer had a little typo in it I think
SELECT m.matchId, h.name, a.name
FROM matches m
INNER JOIN teams h ON h.teamId = m.homeTeamId
INNER JOIN teams a ON a.teamId = a.awayTeamId
-- ^ should be m.awayTeamId
But thanks again for your help!
Geoff

Retrieving data from two tables

I have two tables vendors and customers. My problem is that I want to make the search of the name and in result I get the common data from both the tables(if any exist).
Example: If I search the name John and it exits in both the tables so in result I must get the gridviews of both the tables.
Your question is not quite clear for me. Are you looking for a SQL statement?
Try with:
SELECT * FROM Vendors LEFT JOIN Customers ON Vendors.Name = Customers.Name
or
SELECT * FROM Vendors WHERE Vendors.Name IN (SELECT Customers.Name FROM Customers)

How to fetch data from two different tables?

Good noon to every one
my query is that i have one table name Purchase&Sales and two different field
Purchase
Sales
the data which will be in Purchase text box will be fetch from Total purchase table
and the data will be in sales table will be fetch from Total Sales Table
means the both Value will come from different table to one table
So please Give me a syntax or some idea
Hoping for your Great and positive response
select sum(Purchase) Result from PurchaseTable
union all
select sum(Sales) Result from SalesTable
Using JOIN or try with ForeignKey concept if any.
SELECT
S.Total, -- selecting from one table
P.Total -- selecting from another table
FROM
Sales S
INNER JOIN -- inner join if you can or similar
Purchase P
ON
S.PurchaseId = P.ID
see here for more info http://www.techrepublic.com/article/sql-basics-query-multiple-tables/

Resources