How to Execute a SQL Query Only if Another SQL Query has no Results

I stumbled upon an interesting question on Stack Overflow recently. A user wanted to query a table for a given predicate. If that predicate returns no rows, they wanted to run another query using a different predicate. Preferably in a single query. Challenge accepted! Canonical Idea: Use a Common Table Expression We're querying the Sakila … Continue reading How to Execute a SQL Query Only if Another SQL Query has no Results

When to Use Bind Values, and When to Use Inline Values in SQL

Users of jOOQ, PL/SQL, T-SQL are spoiled as they hardly ever need to worry about bind values. Consider the following statements: Using jOOQ public int countActors(String firstName, String lastName) { return ctx.selectCount() .from(ACTOR) .where(ACTOR.FIRST_NAME.eq(firstName)) .and(ACTOR.LAST_NAME.eq(lastName)) .fetchOneInto(int.class); ); } The method parameters firstName and lastName will be automatically mapped to bind values in the generated SQL … Continue reading When to Use Bind Values, and When to Use Inline Values in SQL

How to Generate at Least One Row in SQL

There are some situations where you would like to have at least one (empty) row in your result set in SQL. Imagine the following situation. We're querying the Sakila database for actors and their films: SELECT first_name, last_name, title FROM actor JOIN film_actor USING (actor_id) JOIN film USING (film_id) ORDER BY 1, 2, 3 yielding … Continue reading How to Generate at Least One Row in SQL

10 Nice Examples of Writing SQL in Kotlin With jOOQ

Kotlin is the next big thing. With Google announcing official support for Kotlin on Android, we'll see a lot more traction for this lovely language. https://twitter.com/StackOverflow/status/864934838037622785 We've already blogged about the Kotlin language recently: 10 Features I Wish Java Would Steal From the Kotlin Language. Should you migrate your application to Kotlin? Perhaps - there … Continue reading 10 Nice Examples of Writing SQL in Kotlin With jOOQ

10 Tips on How to be a Great Programmer

I was recently asked in an interview about my opinion on how to be a great programmer. That's an interesting question, and I think we can all be great programmers, regardless of our talent, if we follow a couple of rules that - I believe - should be common sense. In fact, these rules don't … Continue reading 10 Tips on How to be a Great Programmer

The Difficulty of Tuning Queries Over a Database Link – Or How I Learned to Stop Worrying and Love the DUAL@LINK Table

A large-ish customer in banking (largest tables on that particular system: ~1 billion rows) once decided to separate the OLTP database from the "log database" in order to better use resources and prevent contention on some tables, as the append-only log database is used heavily for analytic querying of all sorts. That seems to make … Continue reading The Difficulty of Tuning Queries Over a Database Link – Or How I Learned to Stop Worrying and Love the DUAL@LINK Table

Don’t Use the String Concatenation “Trick” in SQL Predicates

In SQL, quite often, we want to compare several values with each other. For instance, when we're looking for a specific user by their first and last names, we'll write a query like this one: SELECT * FROM customer WHERE first_name = 'SUSAN' AND last_name = 'WILSON'; We're getting: CUSTOMER_ID FIRST_NAME LAST_NAME ------------------------------------ 8 SUSAN … Continue reading Don’t Use the String Concatenation “Trick” in SQL Predicates