入門

查詢所有列

select * from user_profile诫睬;

查詢用戶的設(shè)備id對應的性別没炒、年齡和學校的數(shù)據(jù)

select device_id,gender,age,university from user_profile;

從用戶信息表中取出學校的去重數(shù)據(jù)

select distinct university from user_profile ;

查看前2個用戶明細設(shè)備ID數(shù)據(jù)

select device_id from user_profile limit 2;

查看后10個用戶明細設(shè)備ID數(shù)據(jù)

select device_id from user_profile limit 10,-1; //11行到last

查看前2個用戶明細設(shè)備ID數(shù)據(jù)药版,并將列名改為 'user_infos_example'

select device_id as user_infos_example from user_profile limit 2;

用戶年齡升序查看信息

select device_id, age from user_profile order by age asc;

用戶年齡降序查看信息

select device_id, age from user_profile order by age desc;

并先按照gpa升序排序,再按照年齡升序排序輸出

select device_id, gpa,age from user_profile order by gpa asc, age asc;

查找2021年8月份所有練習過題目的總用戶數(shù)和練習過題目的總次數(shù)

select count(distinct device_id) as did_cnt, count(question_id) as question_cnt
from question_practice_detail
where date like '2021-08%';

查找所有北京大學的學生

select device_id, university from user_profile where university = '北京大學';

查找24歲以上的用戶

select device_id, gender, age, university from user_profile where age > 24;

查找20歲及以上且23歲及以下的用戶

select device_id, gender, age from user_profile where age >= 20 and age <= 23 ;
select device_id, gender, age from user_profile where age between 20 and 23 ;

查看除復旦大學以外的所有用戶明細

select device_id, gender, age, university from user_profile where university != '復旦大學';
select device_id, gender,age,university from user_profile where university not in("復旦大學")

查看年齡值不為空的用戶信息

select device_id, gender, age, university from user_profile where age is not null;

查找gpa在3.5以上(不包括3.5)的山東大學用戶 或 gpa在3.8以上(不包括3.8)的復旦大學同學

select device_id, gender, age, university, gpa from user_profile
where (university = '山東大學' and gpa > 3.5) or (university = '復旦大學' and gpa > 3.8);

查找復旦大學gpa最高值

select max(gpa) from user_profile where university = '復旦大學';

查找男性用戶有多少人以及他們的平均gpa

select
count(gender) as male_num,
round(avg(gpa), 1) as avg_gpa
from user_profile where gender="male";

對每個學校不同性別的用戶活躍情況和發(fā)帖數(shù)量進行分析呻此,請分別計算出每個學校每種性別的用戶數(shù)备禀、30天內(nèi)平均活躍天數(shù)和平均發(fā)帖數(shù)量。

select gender, university, count(device_id) as user_num,
avg(active_days_within_30) as avg_active_days,
avg(question_cnt) as avg_question_cnt
from user_profile group by gender, university;

where 和 having的區(qū)別

執(zhí)行時機不一樣:where 是分組之前進行限定感论,不滿足where條件绅项,則不參與分組,而having是分組之后對結(jié)果進行過
濾比肄。
可判斷的條件不一樣:where 不能對聚合函數(shù)進行判斷快耿,having 可以。

請取出平均發(fā)貼數(shù)低于5的學蟹技ǎ或平均回帖數(shù)小于20的學校

select university,
avg(question_cnt) as avg_question_cnt,
avg(answer_cnt) as avg_answer_cnt
from user_profile group by university
having avg_question_cnt < 5 or avg_answer_cnt < 20;

不同大學的用戶平均發(fā)帖情況掀亥,并期望結(jié)果按照平均發(fā)帖情況進行升序排列

select university, avg(question_cnt) as avg_question_count
from user_profile
group by university
order by avg_question_count asc;

查看所有來自浙江大學的用戶題目回答明細情況

select device_id,question_id,result
from question_practice_detail
where device_id in(
select device_id from user_profile
where university = '浙江大學'
)
order by question_id;

每個學校答過題的用戶平均答題數(shù)量情況

//顯示內(nèi)連接
select university,
count(question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from question_practice_detail as qpd
inner join user_profile as up
on qpd.device_id=up.device_id
group by university
//隱式內(nèi)連接
select university, count(question_id)/count(distinct qpd.device_id)
from user_profile as up, question_practice_detail as qpd
where up.device_id = qpd.device_id
group by university

查看學校為山東大學或者性別為男性的用戶的device_id、gender妥色、age和gpa數(shù)據(jù)

select device_id, gender, age, gpa
from user_profile
where university = '山東大學'

union all

select device_id, gender, age, gpa
from user_profile
where gender = 'male'

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末搪花,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子嘹害,更是在濱河造成了極大的恐慌撮竿,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件笔呀,死亡現(xiàn)場離奇詭異幢踏,居然都是意外死亡,警方通過查閱死者的電腦和手機许师,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門房蝉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人枯跑,你說我怎么就攤上這事惨驶。” “怎么了敛助?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵粗卜,是天一觀的道長。 經(jīng)常有香客問我纳击,道長续扔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任焕数,我火速辦了婚禮纱昧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘堡赔。我一直安慰自己识脆,他們只是感情好,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著灼捂,像睡著了一般离例。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上悉稠,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天宫蛆,我揣著相機與錄音,去河邊找鬼的猛。 笑死耀盗,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的卦尊。 我是一名探鬼主播叛拷,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼猫牡!你這毒婦竟也來了胡诗?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤淌友,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后骇陈,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體震庭,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年你雌,在試婚紗的時候發(fā)現(xiàn)自己被綠了器联。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡婿崭,死狀恐怖拨拓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情氓栈,我是刑警寧澤渣磷,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站授瘦,受9級特大地震影響醋界,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜提完,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一形纺、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧徒欣,春花似錦逐样、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽秽澳。三九已至,卻和暖如春戏羽,著一層夾襖步出監(jiān)牢的瞬間担神,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工始花, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留妄讯,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓酷宵,卻偏偏與公主長得像亥贸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子浇垦,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

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

  • 觀其大綱 page 01 基礎(chǔ)知識 1 MySQL數(shù)據(jù)庫概要 2 簡單MySQL環(huán)境 3 數(shù)據(jù)的存儲和獲取 4 M...
    周少言閱讀 3,158評論 0 33
  • 一炕置、基本概念解釋 二、MongoDB 數(shù)據(jù)類型 下表為MongoDB中常用的幾種數(shù)據(jù)類型男韧。 ObjectId類似唯...
    為技術(shù)瘋狂閱讀 770評論 0 0
  • pandas Pandas是線上服務類型朴摊,數(shù)據(jù)分析和數(shù)據(jù)處理(在機器學習中數(shù)據(jù)處理) 數(shù)據(jù)分析三劍客: numpy...
    Galaxy_saturn閱讀 759評論 0 1
  • DDL語句(數(shù)據(jù)定義語句) 一甚纲、創(chuàng)建表格 創(chuàng)建表格: create table 表名(字段1 字段類型 字段約束,...
    伯wen閱讀 493評論 0 0
  • ElasticSearch筆記 前言 Elasticsearch 是一個開源的搜索引擎,建立在一個全文搜索引擎庫 ...
    fanyank閱讀 1,082評論 0 2