A common table expression (CTE) in SQL Server is used to create a temporary result set that can be referenced within a SELECT
, INSERT
, UPDATE
, or DELETE
statement. A CTE can simplify complex queries and improve readability. However, there are specific rules for using the ORDER BY
clause inside a CTE.
Fix “Column names in each table must be unique…” in SQL Server (Error 2705)
If you’re getting an error that reads something like “Column names in each table must be unique. Column name ‘c1’ in table ‘t1’ is specified more than once” in SQL Server, it appears that you’re trying to create a table with duplicate column names, or you’re trying to add a column to a table that already has a column of that name.
Column names must be unique within each table.
Continue readingPossible Reason You’re Getting Error 156 When Running a Subquery in SQL Server
Error 156 in SQL Server is a generic error that doesn’t tell us much, other than the fact that we’re using the wrong syntax.
But if you’re running a subquery, and you’re getting error 156, which reads “Incorrect syntax near the keyword ‘WHERE’.” or whatever keyword it’s near, it could be that you haven’t declared an alias for the subquery.
Continue readingHow to Create a UNIQUE Constraint in MySQL
In MySQL, a UNIQUE
constraint is a constraint type that ensures that all values in a column or a group of columns are distinct from each other. In other words, all values that go into the column or group of columns must be unique.
UNIQUE
constraints can be applied whenever we want to prevent duplicate entries in specific columns without making them a primary key.
JOIN ON vs USING vs NATURAL JOIN: What’s the Difference?
Probably the most common way to join tables in SQL is with the ON
clause. But that’s not the only way.
We can also join with the USING
clause, which can be more concise, while providing the same or similar results. And there’s also the concept of a natural join, which is more concise again.
Let’s take a look at these three join options and compare them side by side.
Continue readingUsing the OUTPUT Clause To Log the Result of a MERGE Operation in SQL Server
The MERGE
statement is a versatile feature introduced in SQL Server 2008 that allows the combination of INSERT
, UPDATE
, and DELETE
operations into a single statement. When paired with the OUTPUT
clause, it becomes even more powerful by enabling us to capture the results of these actions, providing us visibility into what changes occurred during the merge.
In this article, we’ll walk through an example that uses the OUTPUT
clause during a MERGE
operation in SQL Server.
Changing the Default Fill Factor Doesn’t Work? Check These Two Things.
If you’ve gone through the steps for changing the default fill factor in SQL Server, but it doesn’t seem to be taking effect on newly created indexes, it could be because you’ve missed one or two crucial steps.
This issue can also be seen by comparing the value
and value_in_use
columns when querying the sys.configuration
view or using sp_configure
to view the current setting.
Why You Might be Getting the 4104 Error When Using the OUTPUT Clause in SQL Server
You may be familiar with SQL Server error 4104 that reads something like “The multi-part identifier “DELETED.Name” could not be bound“, which tells us that the “multi-part identifier” couldn’t be bound.
You may have seen this error when performing joins across tables when using the wrong table prefix for a column or using the table name instead of its alias. But that’s not the only place we can get this error.
If you’re getting this error while using the OUTPUT
clause (which we can use during INSERT
, UPDATE
, DELETE
, or MERGE
operations), then it could be that you’ve accidentally used the wrong prefix for the affected column/s.
PostgreSQL \di Command Explained
The \di
command can be used to list indexes in the current database when using psql
(PostgreSQL’s command-line tool). This meta-command can display all indexes, or just indexes that match certain patterns. Here’s an in-depth explanation of how \di
works, its options, and examples of its various uses.
Quick Query to Identify Unused Indexes in SQL Server
Below is a query that we can use to quickly identify unused indexes in SQL Server. More specifically, it returns indexes that are updated, but not used.
Continue reading