sql經典語句

2022-02-26 10:05:40 字數 2255 閱讀 1545

1.表形式如下:

year       salary

2000        1000

2001        2000

2002        3000

2003        4000

想得到如下形式的查詢結果

year       salary

2000       1000

2001       3000

2002       6000

2003       10000

sql語句怎麼寫?

解答:select year, (select sum(salary) from ta where year <= a.year) as salary  from ta a

2.表結構: 

year          month        amount 

1991      1         1.1       

1991      2         1.2       

1991      3         1.3       

1992      1         2.1       

1992      2         2.2       

1992      3         2.3     

顯示結果: 

year          m1               m2             m3   

1991            1.1               1.2             1.3       

1992            2.1               2.2             2.3       

sql的寫法: 

select year, 

(select amount from test as m where month='1' and test.year=m.year) as m1, 

(select amount from test as m where month='2' and test.year=m.year) as m2, 

(select amount from test as m where month='3' and test.year=m.year) as m3 

from test group by year 

3.請教乙個面試中遇到的sql語句的查詢問題 

表中有a b c三列,用sql語句實現:當a列大於b列時選擇a列否則選擇b列,當b列大於c列時選擇b列否則選擇c列。 

------------------------------------------ 

select (case when a>b then a else b end ), 

(case when b>c then b esle c end) 

from table_name 

4.面試題:乙個日期判斷的sql語句? 

請取出tb_send表中日期(sendtime欄位)為當天的所有記錄?(sendtime欄位為datetime型,包含日期與時間) 

------------------------------------------ 

select * from tb where datediff(dd,sendtime,getdate())=0 

5.有一張表,裡面有3個字段:語文,數學,英語。其中有3條記錄分別表示語文70分,數學80分,英語58分,請用一條sql語句查詢出這三條記錄並按以下條件顯示出來(並寫出您的思路):? 

大於或等於80表示優秀,大於或等於60表示及格,小於60分表示不及格。? 

顯示格式:? 

語文 數學 英語? 

及格 優秀 不及格? 

------------------------------------------ 

select 

(case when 語文》=80 then '優秀' 

when 語文》=60 then '及格' 

else '不及格') as 語文, 

(case when 數學》=80 then '優秀' 

when 數學》=60 then '及格' 

else '不及格') as 數學, 

(case when 英語》=80 then '優秀' 

when 英語》=60 then '及格' 

else '不及格') as 英語, 

from table

sql經典語句

說明 複製表 只複製結構,源表名 a 新錶名 b access可用 方法一 select into b from a where 1 1 方法二 select top 0 into b from a 注意 複製的新錶中的所有欄位都將沒有預設值,即使源表中有設預設值 說明 一條sql 語句搞定資料庫分...

經典sql語句

經典sql語句大全 update 有關update,急!在oracle資料庫中 表 a id firstname,lastname 表 b id,lastname 表 a 中原來id,firstname兩個欄位的資料是完整的 表 b中原來id,lastname兩個欄位的資料是完整的 現在要把表 b中...

經典SQL語句

假設只有乙個table,名為pages,有四個字段,id,url,title,body。裡面儲存了很多網頁,網頁的url位址,title和網頁的內容,然後你用乙個sql查詢將url匹配的排在最前,title匹配的其次,body匹配最後,沒有任何字段匹配的,不返回。如下 用union 實現聯合查詢,在...