Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 501247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:10:12+00:00 2026-05-13T06:10:12+00:00

I often find myself in need of very complex SQL examples when testing abstraction

  • 0

I often find myself in need of very complex SQL examples when testing abstraction concepts or just comparing database styles and structures while working with PostgreSQL, MySQL, and even SQLite.

I assume that means there are others in need of insane queries to open our eyes to what is possible and insure our DB layers can handle anything we throw at them.

So, can anyone share some queries that would through even the most hardcore, ORM-all-the-way guy for a loop?

PostgreSQL

SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
    * | expression [ [ AS ] output_name ] [, ...]
    [ FROM from_item [, ...] ]
    [ WHERE condition ]
    [ GROUP BY expression [, ...] ]
    [ HAVING condition [, ...] ]
    [ WINDOW window_name AS ( window_definition ) [, ...] ]
    [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
    [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start [ ROW | ROWS ] ]
    [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
    [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]

MySQL

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name'
        [CHARACTER SET charset_name]
        export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:10:12+00:00Added an answer on May 13, 2026 at 6:10 am

    Check out this talk during OpenSQLCamp, November 2009.

    Title: SQL For the Insane

    Also
    Recursion with SQL

    The Problem

    Suppose you are tracking supplies and have a field called si_item and another called si_parentid. The parent keeps track of what subclass a supply item belongs to. E.g. you have paper parent that has subclasses such as recycled, non-recycled. When someone takes supplies, you want to return the fully qualified name e.g. Paper->Recycled->20 Lb

    Solution

    CREATE TABLE supplyitem(si_id integer PRIMARY KEY, si_parentid integer, si_item varchar(100));
    
    --load up the table (multirow constructor introduced in 8.2)
    INSERT INTO supplyitem(si_id,si_parentid, si_item)
    VALUES (1, NULL, 'Paper'),
    (2,1, 'Recycled'),
    (3,2, '20 lb'),
    (4,2, '40 lb'),
    (5,1, 'Non-Recycled'),
    (6,5, '20 lb'),
    (7,5, '40 lb'),
    (8,5, 'Scraps');
    
    --Recursive query (introduced in 8.4 returns fully qualified name)
    WITH RECURSIVE supplytree AS
    (SELECT si_id, si_item, si_parentid, CAST(si_item As varchar(1000)) As si_item_fullname
    FROM supplyitem
    WHERE si_parentid IS NULL
    UNION ALL
    SELECT si.si_id,si.si_item,
        si.si_parentid,
        CAST(sp.si_item_fullname || '->' || si.si_item As varchar(1000)) As si_item_fullname
    FROM supplyitem As si
        INNER JOIN supplytree AS sp
        ON (si.si_parentid = sp.si_id)
    )
    SELECT si_id, si_item_fullname
    FROM supplytree
    ORDER BY si_item_fullname;
    

    Result looks like

    si_id |      si_item_fullname
    ------+-----------------------------
     1    | Paper
     5    | Paper->Non-Recycled
     6    | Paper->Non-Recycled->20 lb
     7    | Paper->Non-Recycled->40 lb
     8    | Paper->Non-Recycled->Scraps
     2    | Paper->Recycled
     3    | Paper->Recycled->20 lb
     4    | Paper->Recycled->40 lb
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find myself storing data in a mysql database, and then wanting to
I often find myself in need of writing down a future Visual Studio solution
This is a very simple question. I often find myself wanting to create a
I find myself often with a situation where I need to perform an operation
I often find myself wanting to just print (using the implicit toString() of each
I find myself very often in the situation that I open an element in
When I write SQL queries, I find myself often thinking that there's no way
I often* find myself in need of a data structure which has the following
Often I find myself filling ASP.NET repeaters with items that need the CSS class
I find myself writing this class often in my python code when I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.