Excellent: exquisite SQL statements

Source: Internet
Author: User
Note: copy a table (only copy structure, source table name: a new table name: B)
SQL: Select * into B from a where 1 <> 1

Description: copy a table (copy data, source table name: A target table name: B)
SQL: insert into B (a, B, c) Select D, E, F from B;

Note: DisplayArticle, Submitted and 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: External join query (table name 1: Table A 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

Note: Five minutes in advance of the schedule
SQL: Select * from Schedule where datediff ('minute ', F Start Time, getdate ()> 5

Note: Two associated tables are used to delete information that is already unavailable to the primary table in the secondary table.
SQL:
Delete from info where not exists (select * From infobz where info. INFID = infobz. INFID)

Note :--
SQL:
Select. num,. 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 ') interval between '/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. num = B. num

Note :--
SQL:
Select * From studentinfo where not exists (select * from student where studentinfo. id = student. ID) and Department name = '"& strdepartmentname &"' and major name = '"& strinclusionname &" 'order by gender, student source, total score of the College Entrance Examination

Note:
Go to the telephone fee statistics of each unit from the database for one year (two sources of the fixed telephone fee congratulation fertilizer List)
SQL:
Select. userper,. tel,. standbench, to_char (. telfeedate, 'yyyy') as telyear, sum (decode (to_char (. telfeedate, 'mm'), '01',. factration) as Jan, sum (decode (to_char (. telfeedate, 'mm'), '02',. factration) as Fri, sum (decode (to_char (. telfeedate, 'mm'), '03',. factration) as MAR, sum (decode (to_char (. telfeedate, 'mm'), '04 ',. factration) as Apr, sum (decode (to_char (. telfeedate, 'mm'), '05 ',. factration) as may, sum (decode (to_char (. telfeedate, 'mm'), '06',. factration) as Jue, sum (decode (to_char (. telfeedate, 'mm'), '07 ',. factration) as Jul, sum (decode (to_char (. telfeedate, 'mm'), '08 ',. factration) as Agu, sum (decode (to_char (. telfeedate, 'mm'), '09',. factration) as SEP, sum (decode (to_char (. telfeedate, 'mm'), '10',. factration) as Oct, sum (decode (to_char (. telfeedate, 'mm'), '11',. factration) as Nov, sum (decode (to_char (. telfeedate, 'mm'), '12',. factration) as DEC from (select. userper,. tel,. standyet, B. telfeedate, B. factration from telfeestand A, telstmb where. tel = B. telfax) a group by. userper,. tel,. standbench, to_char (. telfeedate, 'yyyy ')

Notes: Four-table join query problems:
SQL: Select * from a left inner join B on. A = B. B right inner join C on. A = C. C inner join D on. A = D. d Where .....

Note: Obtain 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. handleid-1 from handle)

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.