Task solution help request

  • Couldy You help me with this task, please?

    You are working with the library books database.

    The Books table has the columns id, name, year.

    The library has new books whose information is stored in another table called "New", however they do not have a year column.

     

    Write a query to select the books from both tables, Books and New, combining their data. For the year column of the New books use the value 2022.

    Also, select only the books that are released after the year 1900.

    The result set should contain the name and year columns only, ordered by the name column alphabetically.

    My solution:

    SELECT name, (2002 AS year)

    FROM New

    UNION

    SELECT name, year

    FROM Books

    WHERE year > 1900

    ORDER BY name ASC

    I do really not know how to correct it to be done in a properway

  • You can't generally define aliases INSIDE expressions.

    Drew

    PS: You're dates don't match.

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Did the query you wrote produce the correct results? What was wrong with it?

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • I do not know how to change column name from "yeatr" to proper item. Everything is OK apart from this column name.

  • If you drop the brackets around (2002 AS year), it would work. Alternatively, you could SELECT from the Books table first and then the SELECT against New would inherit the column names.

  • The column name "yeatr" will be inherited by SELECT for New once you have changed it to the proper item you must choose from the Books table.

    happy wheels

    • This reply was modified 1 week, 5 days ago by  adamantload.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply