SQL (Structured Query Language) 是具有數據操縱和數據定義等多種功能的數據庫語言,使用SQL語言能夠有效提高計算機應用係統的工作質量與效率。而數據管理作為近年來最熱門的專業,也給SQL的學習帶來了極大的熱度。那麽如何入門並快速學習SQL呢?我們就從掌握以下SQL基本語句開始。今天我們總結了SQL最基礎也是最麵試必備的SQL語句,希望對大家的SQL學習有所幫助。
關係型SQL
基本查看--查看數據庫。表名和表結構-表數據和總數
show status
show databases;
use your_database;
show tables;
DESC your_table;
查詢單列或者多列
select user_birthday from user
查看行以及行數
select * from your_table limit 3;
計數:select count(*) from your_table
排序
1.隻排一列
select user_birthday from user order by user_birthday;
select user_birthday from user order by user_birthday DESC;
2.多列排序,升序ASC是默認的
select * from user order by user_id,user_birthday;
select * from user order by user_id DESC,user_birthday;
select * from user order by user_id ,user_birthday DESC;
篩選以及過濾
過濾行以及查找 where in not like
select user_birthday from user where id>3;
select user_birthday from user where id like '3%';
去重
select distinct user_birthday from user
字符串和數值操作
拚接:select name||id as tiele from tableA ;
求和:select sum(field1) as sumvalue from tableA
平均:select avg(field1) as avgvalue from tableA
最大:select max(field1) as maxvalue from tableA
最小:select min(field1) as minvalue from tableA
分組和匯總
select count(*) from tableA group by sex;
select id,count(*) from tableA group by sex;
過濾分組
select id,count(*) from tableA group by sex having count(*)>2;
嵌套查詢
select name,sex,id from tableA where id in (select id from tableB where id >3)
聯結查詢--以列為單位對表進行聯結
select name,sex,id from tableA ,tableB where tableA.id = tableB.id;
select name,sex,id from tableA inner join tableB on tableA.id = tableB.id;
select name,sex,id from tableA ,tableB,tableC where tableA.id = tableB.id and tableC.hot = tableB.hot;
組合查詢--以行位單位對表進行操作
select name,sex,id from tableA union select name,sex,id from tableB where tableB.id>3;
select name,sex,id from tableA union all select name,sex,id from tableB where tableB.id>3;
使用索引以及存儲過程
說明
join 是兩張表做交連後裏麵條件相同的部分記錄產生一個記錄集,
union是產生的兩個記錄集(字段要一樣的)並在一起,成為一個新的記錄集
幫助係統
help
help index
?index
退出
Exit
Hive中SQL基本語句
HQL---進入Hive中類 SQL 查詢語言-關係型分析查詢作業
進入
su hdfs
hbase shell
查看已存在的表
show databases;
show tables;
SHOW TABLES 'page.*';
查看表中有多少分區:
show partitions logs;
查看invites的表的結構
DESCRIBE invites;
查看部分行--查詢的結果是隨機選擇的
SELECT foo FROM invites limit 3;
查看分區部分行
SELECT a.foo FROM invites a limit 3;
查看行以及行數
SELECT * FROM invites limit 3;
計數 select count(*) from invites;
排序
在分布式中的排序有所不同---全局排序和reduced有關
order by
sort by
distribute by
cluster By
篩選以及過濾
SELECT a.foo FROM invites a WHERE a.ds='2008-08-15';
去重查詢
Hive上一個典型表內除重的寫法
select ad ,sum(plus),count(distinct name,id) from invites;
分組和匯總
SELECT a.bar, count(*) WHERE a.foo > 0 GROUP BY a.bar;
SELECT year(ymd), avg(price_close) FROM stocks WHERE exchange = 'NASDAQ' AND symbol = 'AAPL' GROUP BY year(ymd) HAVING avg(price_close) > 50.0;
聯結
SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);
SELECT sales.*, things.* FROM sales LEFT OUTER JOIN things ON (sales.id = things.id);
幫助係統
顯示所有函數:
show functions;
查看函數用法:
describe function substr;
退出
exit;
由於專業性和英語學習,計算機專業對於留學生來說也是充滿挑戰的學科,想要進一步學好CS,可以尋求 online tutoring 等學術資源。
https://www.studygate.com/blog-cn/programming%E7%BC%96%E7%A8%8B%E4%BB%A3%E5%86%99/
https://www.studygate.com/blog-cn/%E4%BB%A3%E7%A0%81coding%E4%BB%A3%E5%86%99/