开发者

How do you think while formulating Sql Queries. Is it an experience or a concept?

开发者 https://www.devze.com 2022-12-26 07:08 出处:网络
I have been working on sql server and front end coding and have usually faced problem formulating queries.

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.

Whene开发者_开发百科ver 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 ?


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.


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


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.


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.


  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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消