Subtle SQL statements

Source: Internet
Author: User
Please see the original paste in detail ^_^

Description: Copy table (copy only structure, source table name: A new table name: b)
Sql:select * into B from a where 1<>1

Description: Copy table (copy data, source table name: A target table name: b)
Sql:insert into B (A, B, c) select d,e,f from B;

Description: Displays the article, the author, and the last reply time
Sql:select a.title,a.username,b.adddate from Table A, (select Max (adddate) adddate from table where Table.title=a.title) b

Description: Outer join query (table name 1:a table name 2:b)
Sql:select a.a, A.B, A.C, B.C, B.D, B.f from a left-out JOIN b on a.a = B.C

Description: Schedule five minutes advance reminder
Sql:select * from schedule where DateDiff (' minute ', F start time, GETDATE ()) >5


Description: Two related tables, delete the information in the primary table that has not been in the secondary table
Sql:
Delete from info where NOT EXISTS (SELECT * from Infobz where Info.infid=infobz.infid)

Description:--
Sql:
SELECT A.num, A.name, B.upd_date, b.prev_upd_date
From TABLE1,
(SELECT x.num, x.upd_date, Y.upd_date prev_upd_date
From (SELECT NUM, Upd_date, Inbound_qty, Stock_onhand
From TABLE2
WHERE to_char (upd_date, ' yyyy/mm ') = To_char (sysdate, ' yyyy/mm ')) X,
(SELECT NUM, Upd_date, Stock_onhand
From TABLE2
WHERE to_char (upd_date, ' yyyy/mm ') =
To_char (To_date (To_char (sysdate, ' yyyy/mm ') ¦¦ '/01 ', ' Yyyy/mm/dd ')-1, ' yyyy/mm ')) Y,
WHERE X.num = y.num (+)
and X.inbound_qty + NVL (y.stock_onhand,0) <> X.stock_onhand) B
WHERE A.num = B.num

Description:--
Sql:
SELECT * from Studentinfo where does exists (SELECT * from student where studentinfo.id=student.id) and system name = ' "&strdepart mentname& "' and professional name = ' &strprofessionname& ' ORDER by sex, source of students, college entrance examination total score

Description
To go from the database to the unit telephone fee statistics for one year (telephone number quota, telegram, fertilizer list, two table sources)
Sql:
SELECT A.userper, A.tel, A.standfee, To_char (a.telfeedate, ' yyyy ') as Telyear,
SUM (Decode (a.telfeedate, ' mm '), ' To_char ', a.factration)) as
SUM (Decode (a.telfeedate, ' mm '), ' To_char ', a.factration)) as FRI,
SUM (Decode (a.telfeedate, ' mm '), ' To_char ', a.factration)) as the MAR,
SUM (Decode (a.telfeedate, ' mm '), ' To_char ', a.factration)) as APR,
SUM (Decode (a.telfeedate, ' mm '), ' To_char ', a.factration)) as May,
SUM (Decode (a.telfeedate, ' mm '), ' To_char ', a.factration)) as Jue,
SUM (Decode (To_char (a.telfeedate, ' mm '), ' modified ', a.factration)) as June,
SUM (Decode (a.telfeedate, ' mm '), ' To_char ', a.factration)) as AGU,
SUM (Decode (a.telfeedate, ' mm '), ' To_char ', a.factration)) as SEP,
SUM (Decode (To_char (a.telfeedate, ' mm '), ' ten ', a.factration)) as OCT,
SUM (Decode (To_char (a.telfeedate, ' mm '), ' one ', a.factration)) as NOV,
SUM (Decode (To_char (a.telfeedate, ' mm '), ' a ', a.factration)) as DEC
From (SELECT a.userper, A.tel, A.standfee, B.telfeedate, b.factration
From Telfeestand A, Telfee b
WHERE A.tel = B.telfax) A
GROUP by A.userper, A.tel, A.standfee, To_char (a.telfeedate, ' yyyy ')

Note: Four table joint Inquiry question:
Sql:select * from a left inner join B in a.a=b.b right inner join C in a.a=c.c inner join D on A.A=D.D where ....

Description: Get the smallest unused ID number in the table
Sql:
Select (Case when EXISTS (SELECT * from Handle b WHERE b.handleid = 1) THEN MIN (Handleid) + 1 ELSE 1 end) as Handleid
From Handle
WHERE not Handleid in (SELECT a.handleid-1 from Handle a)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.