1

Without using an explicit JOIN operator ind the Per Capita Income of the Community Area which has a school Safety Score of 1.

    SELECT per_capita_income,community_area_name
    FROM CENSUS_DATA
    where community_area_name =
        (SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1' )

response is "per_capita_income" "community_area_name" there is no value??? Thanks everyone for all response below but the result did not changed, there is no value, however i tried:

 SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS 
where safety_score='1' 

and get the result as 'WASHINGTON PARK' and did seqond query as

SELECT per_capita_income,community_area_name FROM CENSUS_DATA 
where community_area_name = 'WASHINGTON PARK'

I've got again; per_capita_income community_area_name

I've checked below code to confirm and get all per_capita_income values inc. Washington Park

SELECT per_capita_income,community_area_name FROM CENSUS_DATA

so,finally I could not get the capita_income value. The data is in the below links

CENSUS_DATA:

https://ibm.box.com/shared/static/05c3415cbfbtfnr2fx4atenb2sd361ze.csv

CHICAGO_PUBLIC_SCHOOLS

https://ibm.box.com/shared/static/f9gjvj1gjmxxzycdhplzt01qtz0s7ew7.csv

CHICAGO_CRIME_DATA:

https://ibm.box.com/shared/static/svflyugsr9zbqy5bmowgswqemfpm1x7f.csv

adams x
  • 19
  • 1
  • 3
  • 2
    Please execute `SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1'` then pick the result and execute `SELECT per_capita_income,community_area_name FROM CENSUS_DATA where community_area_name = ''` and add the information to your question. BTW you probably want to replace the `=` operator by a `IN` operator. – FXD Jan 30 '19 at 23:00
  • 1
    I'm voting to close this question as off-topic because not using a `JOIN` makes this a homework/quiz question, not a *practical* programming problem. – Damien_The_Unbeliever Jan 31 '19 at 07:01
  • Thanks everyone,I think I solved , when I query small caps as below it worked: ``` SELECT per_capita_income,community_area_name FROM CENSUS_DATA where community_area_name= 'Washington Park' ``` I think I is not equal i in my language settings... – adams x Jan 31 '19 at 10:24

13 Answers13

1
UPDATE CENSUS_DATA SET COMMUNITY_AREA_NAME = UPPER(COMMUNITY_AREA_NAME)  
select  CD.PER_CAPITA_INCOME  
from CENSUS_DATA CD,CHICAGO_PUBLIC_SCHOOLS CPS 
where CD.COMMUNITY_AREA_NAME = CPS.COMMUNITY_AREA_NAME 
and CPS.SAFETY_SCORE = 1

The CENSUS_DATA COMMUNITY_AREA_NAME column has values in lower case and for the CHICAGO_PUBLIC_SCHOOLS COMMUNITY_AREA_NAME column they are in upper case so it's better to update the particular column using UPDATE operation and perform query operation to get the required answer

Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
SAchu47
  • 11
  • 4
1

try this one :

SELECT c.per_capita_income, c.community_area_name
    FROM CENSUS_DATA c, CHICAGO_PUBLIC_SCHOOLS s 
    WHERE s.COMMUNITY_AREA_NUMBER  = c.COMMUNITY_AREA_NUMBER AND s.SAFETY_SCORE = '1' ;
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
0

It looks like you’re looking for an implicit join like this:

SELECT cd.per_capita_income,cd.community_area_name
FROM CENSUS_DATA as cd, CHICAGO_PUBLIC_SCHOOLS as cps
where cd.community_area_name = cps.community_area_name 
AND cps.safety_score='1' 
Nikolaus
  • 1,859
  • 1
  • 10
  • 16
0

As commented by @lau, it is likely that the problem that you are seeing comes from your data.

However, let me also point out that there is an lurking issue with this condition :

WHERE community_area_name =
    (SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1' )

If more than one one record in CHICAGO_PUBLIC_SCHOOLS exists with safety score equal to 1, the equality condition will not properly work (in most RDBMS this results in a runtime error).

This condition should rewritten, either as an IN condition :

SELECT per_capita_income,community_area_name
FROM CENSUS_DATA
WHERE community_area_name IN
    (SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1' )

Or as a correlated subquery with an EXIST condition :

SELECT c.per_capita_income, c.community_area_name
FROM CENSUS_DATA c
WHERE EXISTS (
    SELECT 1 
    FROM CHICAGO_PUBLIC_SCHOOLS s
    WHERE s.community_area_name  = c.community_area_name AND s.safety_score = '1' 
)
GMB
  • 216,147
  • 25
  • 84
  • 135
0

I have looked at the data and as everyone expected, I can confirm there is a problem with your data.

COMMUNITY_AREA_NAME is uppercase in CHICAGO_PUBLIC_SCHOOL It is propercase in CENSUS_DATA.

A string that is lowercase is always different than a string that is UPPERCASE and they are both different from a string that is Propercase.

The correct solution is probably to review your database design and put a foreign key somewhere. We do not have enough details to help on such a thing.

The less correct set everything to propercase for instance and re-run the corrected SELECT.

UPDATE CENSUS_DATA SET community_area_name = INITCAP(community_area_name);
UPDATE CHICAGO_PUBLIC_SCHOOLS  SET community_area_name = INITCAP(community_area_name);


SELECT per_capita_income,community_area_name
    FROM CENSUS_DATA
    where community_area_name IN
        (SELECT community_area_name FROM CHICAGO_PUBLIC_SCHOOLS where safety_score='1' )
FXD
  • 1,960
  • 1
  • 6
  • 9
0
SELECT per_capita_income FROM CENSUS_DATA WHERE upper(community_area_name) IN 
(SELECT "Community_Area_Name" FROM CHICAGO_PUBLIC_SCHOOLS WHERE "Safety_Score" = 1 )
Dipesh Yadav
  • 2,353
  • 1
  • 14
  • 9
0

when are your answer should not contain any join you must use cross product for example if you have 2 tables you must use select * from table1,table2 where condition

in your case, the answer should be

select PER_CAPITA_INCOME from school S,cencus_data C where C.COMMUNITY_AREA_NAME = S.COMMUNITY_AREA_NAME and S.SAFETY_SCORE=1

if it not works change with any other common column between these 2 tables

I hope it is helpful

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
0

Option 1:

select PER_CAPITA_INCOME from CENSUS_DATA 
where UPPER(COMMUNITY_AREA_NAME) = (
    select COMMUNITY_AREA_NAME from CHICAGO_PUBLIC_SCHOOLS where SAFETY_SCORE = 1
);

Option 2:

select PER_CAPITA_INCOME from CENSUS_DATA 
where UPPER(COMMUNITY_AREA_NAME) in (
    select COMMUNITY_AREA_NAME from CHICAGO_PUBLIC_SCHOOLS where SAFETY_SCORE = 1
)

Either should work fine.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

If you are doing the "Databases and SQL for Data Science" course on Coursera. The problem is in the datasets. If you take a look in the census data csv the community name is on lower case, while in the schools csv is on upper case. So this should do the trick

%sql SELECT COMMUNITY_AREA_NAME, PER_CAPITA_INCOME \ FROM CENSUS_DATA \ WHERE UCASE(COMMUNITY_AREA_NAME) = (SELECT "Community_Area_Name" FROM CHICAGO_PUBLIC_SCHOOLS WHERE "Safety_Score" = 1)

0

%sql select community_area_name, per_capita_income from census_data where community_area_name='Washington Park';

0

This worked for me.

%%sql 
select CPS.COMMUNITY_AREA_NAME, CD.PER_CAPITA_INCOME, CPS.SAFETY_SCORE
    from CENSUS_DATA CD, CHICAGO_PUBLIC_SCHOOLS CPS
where CD.COMMUNITY_AREA_NUMBER = CPS.COMMUNITY_AREA_NUMBER 
    and SAFETY_SCORE = 1;
0

the problem is community_area_name in Chicago schools is Upper case while in Census data is lower case so this code worked with me ,,

%sql select per_capita_income,community_area_number,community_area_name from CENUS_DATA where community_area_number in (select community_area_number from SCHOOLS where safety_score=1)

  • 1
    Please consider adding code formatting to your answer. In edit click the {} button after selecting the part that is code – shuberman Jul 09 '20 at 04:25
-1
%%sql select  per_capita_income from CENSUS
where community_area_number = (select community_area_number from SCHOOLS where safety_score = 1)
  • remove the ' from around '1'
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158