SQL connecting 3 seperate tables using procedures - sql

I'm struggling with SQL a bit, so far I have 3 tables:
CUSTOMER
id,
name,
surname,
gender,
birthdate
GENDER
id,
gender
ADRESS
id,
country,
city
I need to create a procedure on connecting these 3 tables into a big one, so it would have (customer id, name, gender, country)
I know that I have to use joins but I am so unfamiliar with SQL.

You should just use a JOIN we use a JOIN on 2 or more tables in order to fetch some common attributes related values of one table that exists in other as together. So, here the sql then would look like. Also you dont require the gender seperate table as you have gender already present in your customer table
Select
c.id, c.name, c.gender, a.country
From CUSTOMER c
Join
Address a
On c.id =a.id

Related

Querying from two tables in Oracle Database

I have two tables where I have students details and other table consists of the details of TAs. Tables are as follows:
Students(B#, first_name, last_name, status, GPA, email, bdate, dept)
TAs(B#, ta_level, office)
Now, For each TA from the CS department, find his/her B#, first name, last name, and birth date. I have tried the following query:
select Students.B#, Students.FIRST_NAME, Students.LAST_NAME, Students.BDATE
from Students INNER JOIN TAs ON Students.B# = TAs.B#;
but I have to get only those TAs who are studying in Computer Science. I am using Oracle DB. How will I add another condition after inner join?
For each TA from the CS department
Is there a table or a column specify if a student is studying Computer Science ? however as per your question it seems from the department you can know that.
You can do the below:
select Students.B#, Students.FIRST_NAME, Students.LAST_NAME, Students.BDATE
from Students INNER JOIN TAs ON Students.B# = TAs.B#
where Students.dept='CS' -- or computer science depending on the value.

what is the proper union or sql construct to resolve these 2 datasets?

I have a table UserParent:
Id, FirstName, LastName
I have a table UserChild:
Id, ParentUserId (FK), ChildAttributeX
I have the following sample SQL:
SELECT Id, 0 ChildUserId, FirstName, LastName, NULL FROM UserParent
UNION
SELECT ParentUserId, Id, FirstName, LastName, ChildAttributeX FROM UserChild
Some Users may exist in both tables. All Users are stored with basic info in UserParent although some Users who have ChildAttributeX will have a FK ref to the UserParent in UserChild along with the ChildAttributeX in UserChild.
How can I resolve this as part of a UNION or some other SQL technique so all Users are included in the result set, without duplicate users?
I think this is what you are looking for. If all records must exist in parent table, this will return all records from parent, and any record that exist in child table, but only unique records (DISTINCT does that).
SELECT DISTINCT UP.ID, UP.FirstName, UP.LastName
FROM UserParent UP
LEFT OUTER JOIN UserChild UC ON UP.ID = UC.ParentUserID
If you are looking for all the records present in both table, you can try below query:
SELECT
coalesce(UP.Id,UC.ParentUserId),
0 ChildUserId,
(UP.FirstName,UC.FirstName),
(UP.LastName,UC.LastName),
NULL FROM
UserParent UP
FULL OUTER JOIN
UserChild UC
ON UC.ParentUserId = UP.ID

Select SQL with multiple tables in Access 2010

Two database tables:
Continent table with ContinentID and ContinentName columns
City table with CityID, CityName and ContinentName columns
Situation:
I want to combine the corresponding city to its continent. Like Europe (continent) has Denmark (country).
However, what's wrong with my SQL statement?
select
CountryID, CountryName
from
Country
where
Country.ContientID = Contient.ContientID;
You actually need to join the tables
select CountryID, CountryName
from Country
inner join Contient on Country.ContientID=Contient.ContientID
You were probably trying the old, legacy implicit join syntax which would work like this
select CountryID, CountryName
from Country, Contient
where Country.ContientID=Contient.ContientID
but you should not use that any more.

Creating table using select statement from multiple tables

I have this university task to create table using SELECT statement from multiple tables, but it's not as simple... here's basic info:
I'm using 2 tables -
CITY(city_ID, name);
PERSON(person_ID, name, surname, city_ID);
--city_ID is FK indicating in which city person was born.
Now my task is to create new table
STATISTICS(city_ID, city_name, number_of_births);
--number_of_births is basically a count of people born in each city
Problem is that I have to use only SELECT statement to do so.
I've tried something like this: (I'm well aware that this cannot possibly work but as to give you a better idea where I'm stuck)
CREATE TABLE Statistics AS
(SELECT city.city_ID, city.name as "city_name", number_of_births AS
(SELECT COUNT(*) FROM person WHERE person.city_id = city.city_id)
FROM city, person);
For SQL Server you can do SELECT * INTO. Something like this:
SELECT
*
INTO Statistics
FROM (
SELECT
city.city_ID,
city.name as "city_name",
(SELECT COUNT(*) FROM person WHERE person.city_id = city.city_id) as 'number_of_births'
FROM city
inner join person on city.city_id = person.city_id
) t1
(Posted on behalf of the question author).
Ok, this got really messy. Dave Zych's answer was correct when rewritten in Oracle dialect.
CREATE TABLE Statistics AS SELECT * FROM (
SELECT DISTINCT
city.city_ID,
city.name AS "City_name",
(SELECT COUNT(*) FROM person WHERE person.city_ID = city.city_ID) AS "number_of_births"
FROM city INNER JOIN person ON city.city_ID = person.city_ID);

In aggregate and grouping expressions, the SELECT clause can contain only aggregates and grouping expressions

I'm new to SQL and I got this exercise for school that I need to do: "List the customer name, CD title and rating for the review (or reviews) with the highest rating".
The database has 5 tables:
CDTitles, Customers, Reviews, OrderLines, Orders
The name of the customer is in the Customers table (2 columns: "firstname" and "lastname"), the CD title in the CDTitles table has "title" and the "rating" on the Reviews table
This is what I got:
SELECT MAX(rating), firstname, lastname, title
FROM Reviews, Customers, CDTitles
WHERE Reviews.customerID = Customers.customerID
AND Reviews.catalogNumber = CDTitles.catalogNumber;
Although I keep getting this error: In aggregate and grouping expressions, the SELECT clause can contain only aggregates and grouping expressions. [ Select clause = ,firstname ].
Any help would be much appreciate it.
PS: I need to use Microsoft Webmatrix.
You have used an aggregate function, but not specified the grouping.
Add a GROUP BY clause:
SELECT
MAX(rating), firstname, lastname, title
FROM Reviews, Customers, CDTitles
WHERE Reviews.customerID = Customers.customerID
AND Reviews.catalogNumber = CDTitles.catalogNumber
GROUP BY firstname, lastname, title -- Added this line
As a matter of style, you should express the joins in the modern style:
SELECT
MAX(rating), firstname, lastname, title
FROM Reviews
JOIN Customers ON Reviews.customerID = Customers.customerID
JOIN CDTitles ON Reviews.catalogNumber = CDTitles.catalogNumber
GROUP BY firstname, lastname, title