Home  >  Q&A  >  body text

How can I reformulate this query without using the AS keyword?

I'm completing a HackerRank challenge, but the documentation says I shouldn't use the AS keyword:

I need to rewrite this query in MySQL so that it does not contain the AS in WITH A AS nor the SELECT...AS test AS

WITH A AS (
    SELECT DISTINCT
        MAX( LENGTH( customer_id ) ) AS test
    FROM
        orders

    UNION

    SELECT DISTINCT
        MIN( LENGTH( customer_id ) )
    FROM
        orders
)
SELECT
    test,
    LENGTH(test)
FROM
    A

P粉765570115P粉765570115277 days ago388

reply all(1)I'll reply

  • P粉497463473

    P粉4974634732024-01-17 14:21:34

    WITH clause is used to declare VIEW, so you can rewrite it like below

    SELECT
        test,
        LENGTH(test)
    FROM
        (
     SELECT DISTINCT
            MAX( LENGTH( customer_id ) ) AS test
        FROM
            orders
    
        UNION
    
        SELECT DISTINCT
            MIN( LENGTH( customer_id ) )
        FROM
            orders)

    reply
    0
  • Cancelreply