Help › Write SQL
Refactor a query
Rename aliases and WITH queries, add an alias, turn a subquery into a WITH query and back, expand a star, and qualify columns, without changing what the statement means.
Querlyn 0.1.0 is not released yet. This help describes it ahead of release.
A refactoring rewrites a statement without changing what it means. Querlyn
knows which name refers to what, so renaming the alias o changes the
references to that alias and leaves a column that happens to be called o
alone.
Refactorings work on the statement the cursor is in. They change the SQL in the editor only. Nothing is sent to the database.
Refactor This
Press ⌃T on macOS or Ctrl+Shift+R on Windows and Linux () to list what can be done where the cursor is. The list holds only what applies there; when nothing applies, a line next to the cursor says why.
All the refactorings are also in the Refactor submenu of the Edit menu, of the Selection menu and of the editor's right-click menu.
| Refactoring | Put the cursor on |
|---|---|
| Rename | An alias, a WITH query's name, or a column alias |
| Introduce Alias | A table in FROM or JOIN that has no alias |
| Extract Subquery to CTE | A subquery |
| Inline CTE | A WITH query's name |
| Expand * to Columns | A * |
| Qualify Columns | Anywhere in the statement |
Rename an alias or a WITH query
- Put the cursor on the name and press F2 ().
- A box opens under the name with the old name selected, and every place the name is used is highlighted.
- Type the new name. Querlyn checks it as you type; if it cannot be used, the box says why in red and ↩ on macOS or Enter on Windows and Linux does nothing.
- Press ↩ on macOS or Enter on Windows and Linux to rename, or ⎋ on macOS or Esc on Windows and Linux to leave everything as it was.
You can rename table aliases, the aliases of subqueries, the names of
WITH queries, column aliases in a select list (with the places that use
them, such as ORDER BY), and the column lists of WITH queries.
A new name that needs quotes in your database, such as a reserved word, gets them; the box says so before you confirm.
Querlyn refuses a rename that would change the statement's meaning, for
example a name another table of the same FROM already has.
Tables, views and their columns live in the database, so they cannot be renamed here. For a table, Querlyn offers Edit Structure… instead; see Create and change tables.
Give a table an alias
adds an alias to the table at the cursor and uses it everywhere the statement refers to that table. This statement:
SELECT name, total
FROM orders
JOIN customers ON customers.id = orders.customer_id;
becomes, with the cursor on orders:
SELECT name, o.total
FROM orders o
JOIN customers ON customers.id = o.customer_id;
The alias is made from the table's initials (order_items becomes oi)
and never clashes with another name in the statement. It then opens in the
rename box, so you can type another one straight away.
Turn a subquery into a WITH query
moves the subquery
at the cursor into a WITH query at the top of the statement. This
statement:
SELECT d.total
FROM (
SELECT customer_id, sum(total) AS total
FROM orders
GROUP BY customer_id
) AS d
WHERE d.total > 100;
becomes:
WITH d AS (
SELECT customer_id, sum(total) AS total
FROM orders
GROUP BY customer_id
)
SELECT d.total
FROM d
WHERE d.total > 100;
The WITH query takes the subquery's alias as its name, or cte when it
has none, and the name opens in the rename box. A subquery used as a value
or after IN or EXISTS becomes (SELECT * FROM cte).
A subquery that refers to the query around it (a correlated subquery) cannot be moved out, and Querlyn says so.
Put a WITH query back in place
does the opposite: it puts the
WITH query's text where the query is used and removes it from WITH.
When the query is used several times, Querlyn shows a preview and asks
whether to inline all uses, and notes that the query then runs once for
each use. A recursive WITH query, and one marked MATERIALIZED, cannot
be inlined.
Expand a star into its columns
replaces * with the
columns it stands for:
SELECT * FROM customers;
becomes, for a table with these five columns:
SELECT id, name, email, country, created_at FROM customers;
When the statement has several tables, each column is written with its table's alias. Querlyn needs to know the tables' columns for this: connect the tab and, if a table changed, refresh it in the sidebar.
Qualify columns
writes every column with the alias or name of its table:
SELECT name, total FROM orders o JOIN customers c ON c.id = customer_id;
becomes:
SELECT c.name, o.total FROM orders o JOIN customers c ON c.id = o.customer_id;
With text selected, only the columns in the selection are qualified. A column Querlyn cannot place with certainty is left as it is, with a note.
Previews and undo
A refactoring that changes more than one place first shows a preview: the changed lines, with the changes highlighted in the editor. Press ↩ on macOS or Enter on Windows and Linux or choose Apply to make the change, ⎋ on macOS or Esc on Windows and Linux or Cancel to drop it. A change in one place is made at once.
Every refactoring is one step for ⌘Z on macOS or Ctrl+Z on Windows and Linux.
Before any refactoring is applied, Querlyn reads the result again and checks that every name still refers to what it referred to before. If it does not, nothing is changed.
Other entries in the Refactor menu
- opens the table designer for the table the cursor is on.
- and belong to lock warnings; see Migration lock warnings.
Help for Querlyn 0.1.0. All help topics