Oracle APEX Join and Count - sql

I have two tables created with SQL code:
CREATE TABLE
TicketSales(
purchase# Number(10),
client# Integer CONSTRAINT fk1 REFERENCES Customers,
PRIMARY KEY(purchase#));
CREATE TABLE Customers(
client# Integer,
name Char(30),
Primary Key(client#);
Basically table TicketSales holds ticket sales data and client# is foreign key referenced in customers table. I would like to count names that are in TicketSales table. i tried below code with no success:
select Count(name)
From Customers
Where Customers.Client#=TicketSales.Client#
Group by Name;
Any help appreciated.
Thanks,

If you want a count by each name, then include name in the select and group by clauses
select c.Name, Count(*)
From Customers c
INNER JOIN TicketSales t ON c.Client# =t.Client#
Group by c.Name;
If you want just the count of names, not tickets, then use
select Count(*)
From Customers c
;
Or, for a count of individuals who have tickets recrded against them:
select Count(DISTINCT t.Client#)
From TicketSales t
;

Related

SQL Query to check if a record does not exist in another table

I have a table which holds details of all Students currently enrolled in classes which looks like this:
CREATE TABLE studentInClass(
studentID int,
classID int,
FOREIGN KEY(studentID) references students(studentID),
foreign key(classID) references class(classID)
);
And another table which contains details of students who have paid for classes:
CREATE TABLE fees(
feesID INTEGER PRIMARY KEY AUTOINCREMENT,
StudentID INTEGER,
AmountPaid INT,
Date DATE,
FOREIGN KEY(StudentID) REFERENCES students(StudentID));
What I want to do is check whether a student who is in a class has not paid for that class. I am struggling to write a SQL query which does so. I have tried multiple queries such as:
Select studentInClass.StudentID
from fees, studentInClass
where fees.StudentID = studentInClass.StudentID;
But this returns no data. I'm not sure how to proceed from here. Any help will be appreciated.
You want outer join :
select s.StudentID, (case when f.AmountPaid is not null
then 'Yes'
else 'No'
end) as Is_fees_paid
from studentInClass s left join
fees f
on f.StudentID = s.StudentID;
With NOT EXISTS:
select s.*
from studentInClass s
where not exists (
select 1 from fees
where studentid = s.studentid
)
with this you get all the rows from the table studentInClass for which there is not the studentid in the table fees.
It's not clear if you also need to check the date.
check it please:
select studentInClass.StudentID
from studentInClass inner join fees ON fees.StudentID = studentInClass.StudentID

Selecting a subset of columns

I am using sqllite to create two tables
CREATE TABLE NAMES(Id integer PRIMARY KEY, Name text);
CREATE TABLE PHONE(Id inetegr PRIMARY KEY, PersonID integer, Number integer,
FOREIGN KEY (PersonID) REFERENCES NAMES(Id)
);
Then, I have written a qry to slect all people having more than 2 phones
select count(PersonID) as counts,Name
from PHONE
INNER JOIN NAMES on NAMES.Id=PHONE.PersonID
group by PersonID
having counts>=2;
The output is like below
2|Tom
However, I dont want to print the count and just need the names, how to change my query to just print the name..
you can try like below
select Name from PHONE
INNER JOIN NAMES on NAMES.Id=PHONE.PersonID
group by Name
having count(distinct Number)>=2;
You can try below
select Name from PHONE
INNER JOIN NAMES on NAMES.Id=PHONE.PersonID
group by Name having count(PersonID)>=2;

SQL - Selecting data from two tables and removing duplicates

So I have two tables and I'm trying to display some data from both and remove the duplicates. Sorry, I'm new to SQL and databases. Here's my code
Table 1
CREATE TABLE customer
(
customer_id VARCHAR2(5),
customer_name VARCHAR2(50) NOT NULL,
customer_address VARCHAR2(150) NOT NULL,
customer_phone VARCHAR2(11) NOT NULL,
PRIMARY KEY (customer_id)
);
Table 2
CREATE TABLE shop
(
shop_id VARCHAR2(7),
shop_address VARCHAR2(150) NOT NULL,
customer_id VARCHAR2(7),
PRIMARY KEY (shop_id),
FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
I want to display everything from the SHOP table, and customer_id, customer_name from the CUSTOMER TABLE.
I've tried this so far, but it's displaying everything from both tables and I get two duplicate customer_id columns:
SELECT *
FROM shop
JOIN customer ON shop.customer_id = customer.customer_id
ORDER BY customer_name;
Anyone able to help?
Thanks
Due to both tables has column customer_id, so you can show everything on shop table and only column customer_name from customer table
SELECT s.*, c.customer_name
FROM shop s
JOIN customer c ON s.customer_id = c.customer_id
ORDER BY c.customer_name;
select distinct c.customer_id, c.customer_name, s.*
from customer c
inner join shop s on c.customer_id = s.customer_id
To remove duplicates, you need to use distinct keyword
https://www.w3schools.com/sql/sql_distinct.asp
You need to manually list the columns you want. Using * will pull in every column from every table. SQL does not have any way of saying "select all columns except these...".
I hope you're only using * casually - it's a very bad idea to use SELECT * inside program code that then expects certain columns to exist in a particular order or with a certain name.
To save typing, you could use * for one of the tables and manually name the rest:
SELECT
customer.*,
shop.shop_id,
shop.shop_address
FROM
...

Selecting columns from different tables

I need to display two columns from my attendance table (MEMBER_ID & MEETING_ID) and one column from my meeting table and finally two columns from my member table which displays the names that match with MEETING_ID.
The attendance table has a composite key (MEMBER_ID*, MEETING_ID*)
The member table's primary key is MEMBER_ID
Meeting table's primary key is MEETING_ID
My attempt is not working, can someone please help?
SELECT MEMBER_ID, MEETING_ID, MEETING_NAME MEMBER_FIRSTNAME, MEMBER_LASTNAME
FROM ATTENDANCE, MEMBER, MEETING
WHERE MEETING.MEMBER_ID = MEETING.MEMBER_ID;
End result needs to be:
MEMBER_ID MEETING_ID MEETING_NAME FIRSTNAME LASTNAME
0001 MEET0004 SPORTS DAY JOHN SMITH
May be you need this.
SELECT A.MEMBER_ID, A.MEETING_ID, M2.MEETING_NAME, M1.MEMBER_FIRSTNAME, M1.MEMBER_LASTNAME
FROM ATTENDANCE A, MEMBER M1, MEETING M2
WHERE M1.MEMBER_ID = A.MEMBER_ID
AND A.MEETING_ID = M2.MEETING_ID;
SELECT
a.MEMBER_ID
,a.MEETING_ID
,mt.MEETING_NAME
,mb.MEMBER_FIRSTNAME
,mb.MEMBER_LASTNAME
FROM
ATTENDANCE a
INNER JOIN MEMBER mb
ON a.MEMBER_ID = mb.MEMBER_ID
INNER JOIN MEETING mt
ON a.MEETING_ID = mt.MEETING_ID
;
Use Explicit Join Syntax and then setup your relationships using the ON conditions and the keys between the tables. Note I also used table aliases to shorten typying.

How to massive update?

I have three tables:
group:
id - primary key
name - varchar
profile:
id - primary key
name - varchar
surname - varchar
[...etc...]
profile_group:
profile_id - integer, foreign key to table profile
group_id - integer, foreign key to table group
Profiles may be in many groups. I have group named "Users" with id=1 and I want to assign all users to this group but only if there was no such entry for the table profiles.
How to do it?
If I understood you correctly, you want to add entries like (profile_id, 1) into profile_group table for all profiles, that were not in this table before. If so, try this:
INSERT INTO profile_group(profile_id, group_id)
SELECT id, 1 FROM profile p
LEFT JOIN profile_group pg on (p.id=pg.profile_id)
WHERE pg.group_id IS NULL;
What you want to do is use a left join to the profile group table and then exclude any matching records (this is done in the where clause of the below SQL statement).
This is faster than using not in (select xxx) since the query profiler seems to handle it better (in my experience)
insert into profile_group (profile_id, group_id)
select p.id, 1
from profiles p
left join profile_group pg on p.id = pg.profile_id
and pg.group_id = 1
where pg.profile_id is null