views:

181

answers:

9

I have been working on sql server and front end coding and have usually faced problem formulating queries.

I do understand most of the concepts of sql that are needed in formulating queries but whenever some new functionality comes into the picture that can be dont using sql query, i do usually fails resolving them.

I am very comfortable with select queries using joins and all such things but when it comes to DML operation i usually fails

For every query that i never done before I usually finds uncomfortable with that while creating them. Whenever I goes for an interview I usually faces this problem.

Is it their some concept behind approaching on formulating sql queries.

Eg.

I need to create an sql query such that

A table contain single column having duplicate record. I need to remove duplicate records. 

I know i can find the solution to this query very easily on Googling, but I want to know how everyone comes to the desired result.

Is it something like Practice Makes Man Perfect i.e. once you did it, next time you will be able to formulate or their is some logic or concept behind.

I could have get my answer of solving above problem simply by posting it on stackoverflow and i would have been with an answer within 5 to 10 minutes but I want to know the reason. How do you work on any new kind of query. Is it a major contribution of experience or some an implementation of concepts.

Whenever I learns some new thing in coding section I tries to utilize it wherever I can use it. But here scenario seems to be changed because might be i am lagging in some concepts.

EDIT

How could I test my knowledge and concepts in Sql and related sql queries ?

A: 

Your example is a test of how well you understand the DISTINCT keyword and the GROUP BY clause, which are SQL's ways of dealing with duplicate data.

Anthony Faull
+2  A: 

On the "Delete a duplicate" example, I'd come to the result by googling it. This scenario is so rare if the DB is designed properly that I wouldn't bother keeping this information in my head. Why bother, when there is a good resource is available for me to look it up when I need it?

For other queries, it really is practice makes perfect.

Over time, you get to remember frequently used patterns just because they ARE frequently used. Rare cases should be kept in a reference material. I've simply got too much other stuff to remember.

David Stratton
@David: I know you are right here. But What if you need to maintain some database that was made by some unexperienced person, and you are given some task that need such headace – Shantanu Gupta 4 secs ago
Shantanu Gupta
Well, I'm pretty much famous in my company for stating "This is flawed from the ground up. We need to re-write this." I realize that's not practical, but I'd revert back to the "If I need to use it frequently, I'll remember it because of repetition. If not, I won't bother." Even in this case, I'd google it once, create a function or stored procedure, and call it when needed.
David Stratton
@David: Sorry, but I am not convinced by you. I am looking for every new tasks that comes into the picture. Like the query listed above is new for me. Although I am a newbie in this field and will learn with my experiences but I feel like if I am clear with my concepts I can do any kind of work that can be done with that particular method. Might be I am lagging on my concepts. But I personally feel that It would be wrong saying search for solution on Google.
Shantanu Gupta
@Shantanu Gupta: Perhaps you could search for the answer on SO instead of google. After all isn't that the point? Once you have searched, you can decide how important that information may be and at that time decide to either save or move on.
galford13x
+5  A: 

Typically, the first time you need to open a child proof bottle of pills, you have a hard time, but after that you are prepared for what it might/will entail.

So it is with programming (me thinks).

You find problems, research best practices, and beat your head against a couple of rocks, but in the process you will come to have a handy set of tools.

Also, reading what others tried/did, is a good way to avoid major obsticles.

All in all, with a lot of practice/coding, you will see patterns quicker, and learn to notice where to make use of what tool.

astander
@astander: So acc to you experience is essential. Is it their something like concepts behind formulating queries
Shantanu Gupta
@astander - Good answer.
David Stratton
Yes, I do believe that experiece is a very large part of spotting the pattern. Very seldomly you would be able to create a *good* query/write good code when not fimiliar with the syntax/thought process. Given a new math problem, you might have trouble initialy knowing where to start, but seeing something that has vaguely a familiar smell, will show you where to start the approach to a solution. Also, experience will teach you pitfalls and dead ends, which will save you a lot of pain/frustration and time.
astander
@astander: thx for your support and david too. I will try my best to come out with your suggestions. All these would help me in my near future.
Shantanu Gupta
+2  A: 

I think in the same way I cook dinner. I have some ingredients (tables, columns etc.), some cooking methods (SELECT, UPDATE, INSERT, GROUP BY etc.) then I put them together in the way I know how.

Sometimes I will do something weird and find it tastes horrible, or that it is amazing.

Occasionally I will pick up new recipes from the internet or friends, then use parts of these in my own.

I also save my recipes in handy repositories, broken down into reusable chunks.

ck
@ck: That's pretty impressive way of explaining. I think I might be lagging in "I put ingredients together in the way I know how." knowing this part. I know what are my ingredients. I know what is the function of salt and pepper and how will they effect. But I think, I am lagging in what order they need to be used. Could you please provide some references relating to this.
Shantanu Gupta
1 up vote - Good one!
CoolBeans
A: 

Examples and experience. You look at other peoples examples and you create your own code and once it groks, you don't need to think about it again.

James Westgate
A: 

I would have a look at the Mere Mortals book - I think it's the one by Hernandez. I remember that when I first started seriously with SQL Server 6.5, moving from manual ISAM databases and Access database systems using VB4, that it was difficult to understand the syntax, the joins and the declarative style. And the SQL queries, while powerful, were very intimidating to understand - because typically, I was looking at generated code in Microsoft Access.

However, once I had developed a relatively systematic approach to building queries in a consistent and straightforward fashion, my skills and confidence quickly moved forward.

Cade Roux
A: 

From seeing your responses you have two options.

  1. Have a copy of the specification for whatever your working on (SQL spec and the documentation for the SQL implementation (SQLite, SQL Server etc..)
  2. Use Google, SO, Books, etc.. as a resource to find answers.

You can't formulate an answer to a problem without doing one of the above. The first option is to become well versed into the capabilities of whatever you are working on.

The second option allows you to find answers that you may not even fully know how to ask. You example is fairly simplistic, so if you read the spec/implementation documentaion you would know the answer right away. But there are times, where even if you read the spec/documentation you don't know the answer. You only know that it IS possible, just not how to do it.

Remember that as far as jobs and supervisors go, being able to resolve a problem is important, but the faster you can do it the better which can often be done with option 2.

galford13x
+1  A: 
  1. Find a good documentation to your software. I am using Mysql a lot and Mysql has excellent documentation site with decent search function so you get many answers just by reading docs. If you do NOT get your answer at least you are learning something.

  2. Than I set up an example database (or use the one I am working on) and gradually build my SQL. I tend to separate the problem into small pieces and solve it step by step - this is very successful if you are building queries including many JOINS - it is best to start with some particular case and "polute" your SQL with many conditions like WHEN id = "123" which you are taking out as you are working towards your solution.

  3. The best and fastest way to learn good SQL is to work with someone else, preferably someone who knows more than you, but it is not necessarry condition. It can be replaced by studying mature code written by others.

gorn
+2  A: 

I have a somewhat methodical method of constructing queries in general, and it is something I use elsewhere with any problem solving I need to do.

The first step is ALWAYS listing out any bits of information I have in a request. Information is essentially anything that tells me something about something.

A table contain single column having duplicate record. I need to remove duplicate

  1. I have a table (I'll call it table1)
  2. I have a column on table table1 (I'll call it col1)
  3. I have duplicates in col1 on table table1
  4. I need to remove duplicates.

The next step of my query construction is identifying the action I'll take from the information I have. I'll look for certain keywords (e.g. remove, create, edit, show, etc...) along with the standard insert, update, delete to determine the action. In the example this would be DELETE because of remove.

The next step is isolation.

Asnwer the question "the action determined above should only be valid for __..?" This part is almost always the most difficult part of constructing any query because it's usually abstract. In the above example you're listing "duplicate records" as a piece of information, but that's really an abstract concept of something (anything where a specific value is not unique in usage). Isolation is also where I test my action using a SELECT statement. Every new query I run gets thrown through a select first!

The next step is execution, or essentially the "how do I get this done" part of a request.

A lot of times you'll figure the how out during the isolation step, but in some instances (yours included) how you isolate something, and how you fix it is not the same thing. Showing duplicated values is different than removing a specific duplicate.

The last step is implementation. This is just where I take everything and make the query...

Summing it all up... for me to construct a query I'll pick out all information that I have in the request. Using the information I'll figure out what I need to do (the action), and what I need to do it on (isolation). Once I know what I need to do with what I figure out the execution.

Every single time I'm starting a new "query" I'll run it through these general steps to get an idea for what I'm going to do at an abstract level. For specific implementations of an actual request you'll have to have some knowledge (or access to google) to go further than this.

Kris

KSimons