NOTE: The execution of this query involves performing one or more Cartesian product joins that can not be optimized.
=
) in the WHERE clause. For more information about comparison operators,
see Retrieving Rows Based on a Comparison. In this example, all U.S. cities in the USCITYCOORDS table
are selected that are south of Cairo, Egypt. The compound WHERE clause
specifies the city of Cairo in the WORLDCITYCOORDS table and joins
USCITYCOORDS and WORLDCITYCOORDS on their Latitude columns, using
a less-than (lt
) operator.
libname sql 'SAS-library';
proc sql;
title 'US Cities South of Cairo, Egypt';
select us.City, us.State, us.Latitude, world.city, world.latitude
from sql.worldcitycoords world, sql.uscitycoords us
where world.city = 'Cairo' and
us.latitude lt world.latitude;
proc sql; title 'One and Two Joined'; select one.a 'One', one.b, two.a 'Two', two.b from one, two where one.b=two.b;
L
from the COUNTRIES table.
libname sql 'SAS-library';
proc sql;
title 'Coordinates of Capital Cities';
select Capital format=$12., Name format=$12.,
City format=$12., Country format=$12.,
Latitude, Longitude
from sql.countries, sql.worldcitycoords
where Capital like 'L%' and
Capital = City;
Capital = City
in
the WHERE expression yields the following incorrect output:
libname sql 'SAS-library';
title 'Coordinates of State Capitals';
proc sql outobs=10;
select us.Capital format=$15., us.Name 'State' format=$15.,
pc.Code, c.Latitude, c.Longitude
from sql.unitedstates us, sql.postalcodes pc,
sql.uscitycoords c
where us.Capital = c.City and
us.Name = pc.Name and
pc.Code = c.State;
libname sql 'SAS-library';
proc sql;
title "Cities' High Temps = Cities' Low Temps";
select High.City format $12., High.Country format $12.,
High.AvgHigh, ' | ',
Low.City format $12., Low.Country format $12.,
Low.AvgLow
from sql.worldtemps High, sql.worldtemps Low
where High.AvgHigh = Low.AvgLow and
High.city ne Low.city and
High.country ne Low.country;
High
and Low
. Conceptually, this makes a copy of the table so
that a join can be made between the table and its copy. The WHERE
clause selects those rows that have high temperature equal to low
temperature.
#
) is used as a
line split character in the labels.
libname sql 'SAS-library';
proc sql outobs=10;
title 'Populations and/or Coordinates of World Cities';
select City '#City#(WORLDCITYCOORDS)' format=$20.,
Capital '#Capital#(COUNTRIES)' format=$20.,
Population, Latitude, Longitude
from sql.countries full join sql.worldcitycoords
on Capital = City and
Name = Country;
libname sql 'SAS-library';
proc sql outobs=6;
title 'Oil Production/Reserves of Countries';
select country, barrelsperday 'Production', barrels 'Reserve'
from sql.oilprod natural join sql.oilrsrvs
order by barrelsperday desc;
proc sql; select a.W, a.X, Y, Z from table1 a left join table2 b on a.W=b.W and a.X=b.X order by a.W;
proc sql; select W, X, Y, Z from table1 natural left join table2 order by W;
libname sql 'SAS-library';
proc sql outobs=10;
title 'Populations and/or Coordinates of World Cities';
select coalesce(Capital, City,Name)format=$20. 'City',
coalesce(Name, Country) format=$20. 'Country',
Population, Latitude, Longitude
from sql.countries full join sql.worldcitycoords
on Capital = City and
Name = Country;
FLTSUPER FLTDEST Flight Supervisor Flight Destination 145 Kang 145 Brussels 150 Miller 150 Paris 155 Evanko 155 Honolulu
data fltsuper; input Flight Supervisor $; datalines; 145 Kang 150 Miller 155 Evanko ; data fltdest; input Flight Destination $; datalines; 145 Brussels 150 Paris 155 Honolulu ; run;
data merged; merge FltSuper FltDest; by Flight; run; proc print data=merged noobs; title 'Table MERGED'; run;
FLTSUPER FLTDEST Flight Supervisor Flight Destination 145 Kang 145 Brussels 150 Miller 150 Paris 155 Evanko 165 Seattle 157 Lei
data merged; merge fltsuper fltdest; by flight; run; proc print data=merged noobs; title 'Table MERGED'; run;
proc sql; select coalesce(s.Flight,d.Flight) as Flight, Supervisor, Destination from fltsuper s full join fltdest d on s.Flight=d.Flight;
FLTSUPER FLTDEST Flight Supervisor Flight Destination 145 Kang 145 Brussels 145 Ramirez 145 Edmonton 150 Miller 150 Paris 150 Picard 150 Madrid 155 Evanko 165 Seattle 157 Lei
Kang
matches with Brussels
and Ramirez
matches with Edmonton
. Because the DATA step merges data based
on the position of values in BY groups, the values of Supervisor and
Destination match appropriately. A DATA step merge produces
Match-Merge of the FLTSUPER and FLTDEST Tables: data merged; merge fltsuper fltdest; by flight; run; proc print data=merged noobs; title 'Table MERGED'; run;
Kang
only with Brussels
, Ramirez
only with Edmonton
, and so on.