Introduction to Writing SQL Queries

Learn about the placement of business logic with regards to databases.

Now, we’re going to learn how to write SQL queries. There are several ways to accomplish this, both from the SQL syntax and semantics points of view, which will be covered later. Here, we want to address how to write SQL queries as part of our application code.

Object-relational mapping (ORM)

Some developers prefer to use an ORMObject-relational mapping (ORM) is a technique that lets us query and manipulate data from a database using an object-oriented paradigm. to write their queries and don’t format, indent, and maintain SQL queries themselves. However, SQL is code, so we need to apply similar rules as when we maintain code written in other languages: indentation, comments, version control, unit testing, etc.

Also, to debug what happens in production, we need to be able to easily spot where the query comes from, replay it, edit it, and update our code with the new fixed version of the query.

Writing software

Before we go into detail about the specifics of those concerns, it might be a good idea to review how SQL actually helps us write software, what parts of the code we’re writing in the database layer, and how much we can or should be writing. The question is this: Is SQL a good place to implement business logic?

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy