sql

SELECT Major,
Median,Unemployed 
FROM recent_grads
WHERE 
Median > 10000 OR 
Unemployed <= 1000
LIMIT 20

SELECT Major_category,
Major,
Unemployment_rate 
FROM recent_grads
WHERE 
Major_category = 'Engineering' OR
 Major_category = 'Physical Sciences'
ORDER BY Unemployment_rate

SELECT Major,
ShareWomen,Unemployment_rate 
FROM recent_grads
WHERE ShareWomen > 0.3 
AND Unemployment_rate < 0.1
ORDER BY ShareWomen DESC

SELECT  COUNT(Major) 
FROM  recent_grads 
WHERE ShareWomen < 0.5
SELECT MIN(ShareWomen) 
FROM recent_grads;
SELECT Major, MIN(ShareWomen)
 FROM recent_grads

Write a query that returns the Engineering major with the lowest median salary.
We only want the Major, Major_category, and MIN(Median) columns in the result.

SELECT Major, Major_category, MIN(Median) 
FROM recent_grads
WHERE Major_category='Engineering'

The final two aggregation functions we'll look at are SUM() and AVG(). Applying the SUM() function will add all of the values in a column while AVG() will compute the average. Lastly, the TOTAL() function also returns the sum as a floating point value (even if the column contains integers). The TOTAL() function should be used when working with a column containing floating point values. You can read more here.

This time around, we're going to skip showing sample code since these functions are used the same way as COUNT(), MIN(), and MAX(). This is good practice working with new functions, as SQL contains many functions that you'll end up using down the road that you haven't been taught explicitly.

Write a query that computes the sum of the Total column. - Return only the total number of students integer value.


Select SUM(Total)
 from recent_grads
SELECT Major, Major_category 
FROM recent_grads
SELECT MIN(Median), 
MAX(Median), 
SUM(Total)
FROM recent_grads

Write a query that computes the average of the Total column, the minimum of the Men column, and the maximum of the Women column, in that specific order.
Make sure that all of the aggregate functions are capitalized (SUM() not sum(), etc), so our results match yours.


SELECT AVG(Total),
MIN(Men),
MAX(Women)
FROM recent_grads

it's helpful to be able to specify custom names for the columns in our results.

We can do just that using the AS operator:

SELECT COUNT(*)
 as num_students
 FROM recent_grads
SELECT COUNT(*) 
as "Total Students" 
FROM recent_grads

Even better, we can drop AS entirely and just add the name next to the original column:

SElECT COUNT(*) 
"Total Students" 
FROM recent_grads

we can reference renamed columns when writing longer queries to make our code more compact:

SELECT Major m, 
Major_category mc, 
Unemployment_rate ur
FROM recent_grads
WHERE (mc = 'Engineering') AND 
(ur > 0.04 and ur < 0.08)
ORDER BY ur DESC

Write a query that returns, in the following order:
the number of rows as Number of Students
the maximum value of Unemployment_rate as Highest Unemployment Rate

SELECT COUNT(*) "Number of Students",
MAX (Unemployment_rate) 'Highest Unemployment Rate'
FROM recent_grads

We can return all of the unique values in a column using the DISTINCT statement

SELECT DISTINCT 
Major_category
 FROM recent_grads

SELECT DISTINCT 
Major, Major_category 
FROM recent_grads 
limit 5
SELECT COUNT(DISTINCT(
Major_category)) 
unique_major_categories 
FROM recent_grads

Write a query that returns the number of unique values in the Major, Major_category, and Major_code columns. Use the following aliases in the following order:
For the unique value count of the Major column, use the alias unique_majors.
For the unique value count of the Major_category column, use the alias unique_major_categories.
For the unique value count of the Major_code column, use the alias unique_major_codes.

SELECT 
COUNT(DISTINCT(Major)) 
unique_majors,
COUNT(DISTINCT(Major_category)) 
unique_major_categories,
COUNT(DISTINCT(Major_code)) 
unique_major_codes
FROM recent_grads

Write a query that computes the difference between the 25th and 75th percentile of salaries for all majors.
Return the Major column first, using the default column name.
Return the Major_category column second, using the default column name.
Return the compute difference between the 25th and 75th percentile third, using the alias quartile_spread.
Order the results from lowest to highest and only return the first 20 results.

SELECT Major,
Major_category,
(P75th-P25th) quartile_spread
FROM recent_grads
ORDER BY quartile_spread
LIMIT 20

To illustrate, we can find the total number of people employed in each major category with the following query:

SELECT 
SUM(Employed)
FROM recent_grads
GROUP BY Major_category
SELET Major_category,
SUM(Employed)
FROM rencent_grads
GROUP BY Major_category

Use the SELECT statement to select the following columns and aggregates in a query:
Major_category
AVG(ShareWomen)
Use the GROUP BY statement to group the query by the Major_category column.


SELECT Major_category,AVG(ShareWomen)
FROM recent_grads
GROUP BY Major_category

For each major category, find the percentage of graduates who are employed.
Use the SELECT statement to select the following columns and aggregates in your query:
Major_category
AVG(Employed) / AVG(Total) as share_employed
Use the GROUP BY statement to group the query by the Major_category column.


SELECT Major_category,
AVG(Employed)/AVG(Total) AS
share_employed
FROM recent_grads
GROUP BY Major_category

We can't use the WHERE clause to do this because share_employed isn't a column in recent_grads; it's actually a virtual column generated by the GROUP BY statement.

SELECT Major_category, 
AVG(Employed) / AVG(Total) AS 
share_employed 
FROM recent_grads 
GROUP BY Major_category 
HAVING share_employed > .8

Find all of the major categories where the share of graduates with low-wage jobs is greater than .1.
Use the SELECT statement to select the following columns and aggregates in a query:
Major_category
AVG(Low_wage_jobs) / AVG(Total) as share_low_wage
Use the GROUP BY statement to group the query by the Major_category column.
Use the HAVING statement to restrict the selection to rows where share_low_wage is greater than .1.

SELECT Major_category,
AVG(Low_wage_jobs)/AVG(Total) AS
share_low_wage
FROM recent_grads
GROUP BY Major_category
HAVING share_low_wage >.1

Write a SQL query that returns the following columns of recent_grads (in the same order):
ShareWomen rounded to 4 decimal places
Major_category
Limit the results to 10 rows.

SELECT ROUND(ShareWomen,4),
Major_category
FROM recent_grads
limit 10
SELECT Major_category,
ROUND(AVG(Employed) / AVG(Total), 3) 
AS share_employed 
FROM recent_grads 
GROUP BY Major_category 
HAVING share_employed > .8;

Use the SELECT statement to select the following columns and aggregates in a query:
Major_category
AVG(College_jobs) / AVG(Total) as share_degree_jobs
Use the ROUND function to round share_degree_jobs to 3 decimal places.
Group the query by the Major_category column.
Only select rows where share_degree_jobs is less than .3.

SELECT Major_category,
ROUND(AVG(College_jobs)/AVG(Total),3) AS
share_degree_jobs
FROM recent_grads
GROUP BY Major_category
HAVING share_degree_jobs < .3

Write a query that divides the sum of the Women column by the sum of the Total column, aliased as SW.
Group the results by Major_category and order by SW.
The results should only contain the Major_category and SW columns, in that order.

SELECT Major_category, 
Cast(SUM(Women) as Float)/Cast(SUM(Total) as Float) SW
FROM recent_grads 
GROUP BY Major_category 
ORDER BY SW
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市牺蹄,隨后出現(xiàn)的幾起案子忘伞,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件氓奈,死亡現(xiàn)場離奇詭異翘魄,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)舀奶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門暑竟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人育勺,你說我怎么就攤上這事但荤。” “怎么了涧至?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵腹躁,是天一觀的道長。 經(jīng)常有香客問我南蓬,道長纺非,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任赘方,我火速辦了婚禮烧颖,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蒜焊。我一直安慰自己倒信,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布泳梆。 她就那樣靜靜地躺著鳖悠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪优妙。 梳的紋絲不亂的頭發(fā)上乘综,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機(jī)與錄音套硼,去河邊找鬼卡辰。 笑死,一個胖子當(dāng)著我的面吹牛邪意,可吹牛的內(nèi)容都是我干的九妈。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼雾鬼,長吁一口氣:“原來是場噩夢啊……” “哼萌朱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起策菜,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤晶疼,失蹤者是張志新(化名)和其女友劉穎酒贬,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體翠霍,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡锭吨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了寒匙。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片零如。...
    茶點(diǎn)故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蒋情,靈堂內(nèi)的尸體忽然破棺而出埠况,到底是詐尸還是另有隱情,我是刑警寧澤棵癣,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布辕翰,位于F島的核電站,受9級特大地震影響狈谊,放射性物質(zhì)發(fā)生泄漏喜命。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一河劝、第九天 我趴在偏房一處隱蔽的房頂上張望壁榕。 院中可真熱鬧,春花似錦赎瞎、人聲如沸牌里。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽牡辽。三九已至,卻和暖如春敞临,著一層夾襖步出監(jiān)牢的瞬間态辛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工挺尿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奏黑,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓编矾,卻偏偏與公主長得像熟史,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子窄俏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,332評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,505評論 0 23
  • 筆者臨北魏楷書拓夲《元懷墓志》 上圖為筆者臨魏碑《蘇孝慈墓志》 清代的金石書風(fēng)興起以故,在中國書法史上形成了個陡然轉(zhuǎn)折...
    臨川文墨閱讀 346評論 3 3
  • 前兩天接到好朋友,許沐的來電裆操,她興奮地跟我說怒详,你知道嗎?7年了踪区,7年了昆烁,我終于有勇氣和他在一起了,他來到我的城市缎岗,...
    清兒姑娘閱讀 557評論 3 2
  • 我常在想生命這個宏大深邃的東西 我以為生命當(dāng)以己為中心 人與人的差別遠(yuǎn)大于人與動物的差別 誰怎么樣那就怎么樣 有的...
    YY月牙彎彎閱讀 175評論 0 6