Another Recruiter Asks a SQL Question

By Bill Graziano on 7 March 2001 | 14 Comments | Tags: Queries


Sandeep writes Hi IT Guys, during an interview, a recruiter asked me "How can you retrieve the first n number of fields using sql statement withought using field names?" ex. Table A contains 50 fields and I want to select first 10 fields. I don't want to mention all the 10 fields name. I love these questions!

We dealt with a recruiter question once before and had fun with it. This is the first recruiter question since then so I thought I'd give it a shot. I'll tell you how I came up with a solution built entirely on articles published on SQLTeam! Let's see if anyone can do better.

Now my job seeking reader didn't tell me (and I asked) what database he's using so I'm going to assume it's Microsoft SQL Server 7.0. However I'll also try to make my solution as ANSI standard as possible. I'll also use the pubs database for this solution.

After reading this question it was pretty obvious we'd have to use some dynamic SQL. Dyamic SQL involves building a string that is valid SQL statement and then executing the contents of the string using EXEC. If you read the FAQ you'll find a couple of articles listed with examples. I looked through Books Online and it didn't mention anything about ANSI compliance with respect to EXEC. However I'm fairly certain most major databases have some way to execute a string that contains a SQL statement.

I knew I also needed to get the names of the tables. You can do that using the INFORMATION SCHEMA views. These are a series of ANSI SQL-92 standard views that you can query to determine your database structure. For example this query:

SELECT 	*
from	INFORMATION_SCHEMA.Columns
where	table_name = 'authors'

will return all the information about all the columns in the authors tables. We can get the first ten column names using this query:

SELECT 	column_name
from	INFORMATION_SCHEMA.Columns
where	table_name = 'authors'
and	ordinal_position <= 10

Now I need to turn that into someting like au_id, au_lname, . . . so I can build my SELECT statement. Fortunately we have a great article on turning a series of rows into a list of comma separated values (CSV). We also have a few other articles on dealing with CSV strings. The article uses the COALESCE function to build the string. I'm not entirely sure about the ANSI-ness of COALESCE but I seem to remember it in there. Maybe some one will submit a link to the ANSI SQL-92 standard.

Which leaves me with this script:

set nocount on

Declare @TableName varchar(128), @ColumnList varchar(1000)
Declare @SQL varchar(1000)
set @TableName = 'authors'

SELECT @ColumnList = COALESCE(@ColumnList + ', ', '') + column_name 
FROM INFORMATION_SCHEMA.Columns
WHERE Ordinal_position <= 10
and table_name = @TableName

Set @SQL = 'Select ' + @ColumnList + ' from ' + @TableName

Exec (@SQL)

I'm pretty sure this isn't what the recruiter had in mind. They always want some three line simple SELECT statment. The only topic I really don't have an article about is declaring, setting and using variables. Oh and the syntax of a SELECT statement. ButI do have some links to them.

You should be able to copy and paste my script into Query Analyzer just fine. So there's an answer for this recruiter using mostly articles from SQLTeam. My new tag line should say "Read SQLTeam. Get a job."

If you have an alternative or better solution please post it in the forums or after this article. I'm curious to see what someone else might come up with. Good luck with that next interview.

Discuss this article: 14 Comments so far. Print this Article.

If you like this article you can sign up for our weekly newsletter. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Related Articles

Using Dynamic SQL in Stored Procedures (7 March 2011)

Joining to the Next Sequential Row (2 April 2008)

Writing Outer Joins in T-SQL (11 February 2008)

Aggregating Correlated Sub-Queries (23 October 2007)

How to Use GROUP BY with Distinct Aggregates and Derived tables (31 July 2007)

How to Use GROUP BY in SQL Server (30 July 2007)

Returning Complex Data from User-Defined Functions with CROSS APPLY (11 June 2007)

Returning a week number for any given date and starting fiscal month (2 May 2007)

Other Recent Forum Posts

Creating a 2nd Schedule Table (2 Replies)

SQL 2014 Text Qualifier for Double quotes (1 Reply)

SQL Server Error (13 Replies)

overload in sql a server Processs is taking much t (1 Reply)

Refactor - Duration/Reads Lower, Writes Higher (10 Replies)

Select CASE (5 Replies)

How to check and assign boolean true false value (3 Replies)

Datareader extracts different number of records (1 Reply)

Subscribe to SQLTeam.com

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email. Subscribers receive our white paper with performance tips for developers.

SQLTeam.com Articles via RSS

SQLTeam.com Weblog via RSS

- Advertisement -