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 7795567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:06:24+00:00 2026-06-01T23:06:24+00:00

Disclaimer: my SQL skills are basic, to say the least. Let’s say I have

  • 0

Disclaimer: my SQL skills are basic, to say the least.

Let’s say I have two similar data types in different tables of the same database.

The first table is called hardback and the fields are as follows:

hbID | hbTitle | hbPublisherID | hbPublishDate

The second table is called paperback and its fields hold similar data but the fields are named differently:

pbID | pbTitle | pbPublisherID | pbPublishDate

I need to retrieve the 10 most recent hardback and paperback books, where the publisher ID is 7.

This is what I have so far:

SELECT TOP 10
    hbID, hbTitle, hbPublisherID, hbPublishDate AS pDate
    bpID, pbTitle, bpPublisherID, pbPublishDate AS pDate
FROM hardback CROSS JOIN paperback
WHERE (hbPublisherID = 7) OR (pbPublisherID = 7)
ORDER BY pDate DESC

This returns seven columns per row, at least three of which may or may not be for the wrong publisher. Possibly four, depending on the contents of pDate, which is almost certainly going to be a problem if the other six columns are for the correct publisher!

In an effort to release an earlier version of this software, I ran two separate queries fetching 10 records each, then sorted them by date and discarded the bottom ten, but I just know there must be a more elegant way to do it!

Any suggestions?

Aside: I was reviewing what I’d written here, when my Mac suddenly experienced a kernel panic. Restarted, reopened my tabs and everything I’d typed was still here! Stack Exchange sites are awesome 🙂

  • 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-06-01T23:06:26+00:00Added an answer on June 1, 2026 at 11:06 pm

    The easiest way is probably a UNION:

    SELECT TOP 10 * FROM
    (SELECT hbID, hbTitle, hbPublisherID as PublisherID, hbPublishDate as pDate
     FROM hardback
     UNION 
     SELECT hpID, hpTitle, hpPublisher, hpPublishDate
     FROM paperback
    ) books
    WHERE PublisherID = 7
    

    If you could have two copies of the same title (1 paperback, 1 hardcover), change the UNION to a UNION ALL; UNION alone discards duplicates. You could also add a column that indicates what book type it is by adding a pseudo-column to each select (after the publish date, for instance):

    hbPublishDate as pDate, 'H' as Covertype
    

    You’ll have to add the same new column to the paperback half of the query, using ‘P’ instead. Note that on the second query you don’t have to specify column names; the resultset takes the names from the first one. All column data types in the two queries have match, also – you can’t UNION a date column in the first with a numeric column in the second without converting the two columns to the same datatype in the query.

    Here’s a sample script for creating two tables and doing the select above. It works just fine in SQL Server Management Studio.Just remember to drop the two tables (using DROP Table tablename) when you’re done.

    use tempdb;
    create table Paperback (pbID Integer Identity, 
        pbTitle nvarchar(30), pbPublisherID Integer, pbPubDate Date);
    create table Hardback (hbID Integer Identity, 
        hbTitle nvarchar(30), hbPublisherID Integer, hbPubDate Date);
    
    insert into Paperback (pbTitle, pbPublisherID, pbPubDate)
      values ('Test title 1', 1, GETDATE());
    insert into Hardback (hbTitle, hbPublisherID, hbPubDate)
      values ('Test title 1', 1, GETDATE());
    
    select * from (
      select pbID, pbTitle, pbPublisherID, pbPubDate, 'P' as Covertype
      from Paperback
      union all
      select hbID, hbTitle, hbPublisherID, hbPubDate,'H' 
      from Hardback) books
    order by CoverType;
    
    /* You'd drop the two tables here with
    DROP table Paperback;
    DROP table HardBack;
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Entity Framework, that's my disclaimer! I have a SQL 2008
Disclaimer: I am completely new to PHP and MySQL/SQL. I have multiple implode() functions
Disclaimer: I have no Twitter API experience nor have I used Twitter until today
Disclaimer: I'm not a c++ developer, I can only do basic things. (I understand
I have a relational record similar to the following: There are users (table users)
Disclaimer: I'm very new to SQL and databases in general. I need to create
I have the following SQL statement which returns Not unique table/alias . I'm fairly
Disclaimer: I have attempted to contact the developer about this plugin, but silence reigns.
I am trying to import data from a FoxPro database into Sql Server however
Disclaimer: Never used php in my life. New to Unix and Jenkins I have

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.