This month's discount Comprehensive Database Performance Health Check · Testimonials
SQLAuthority with Pinal Dave
Home
  • Consulting
    • Health Check
  • Free Videos
  • All Articles
    • AI
    • SQL Performance
    • SQL Tips and Tricks
    • Interview Questions and Answers
    • SQL Puzzle
    • SQL Video
    • SQLAuthority News
    • Personal
  • Books
  • Consulting
Thirteen seedlings listed with the number of plants in their bed. The five North plants show 5, the three South plants and three East plants show 3, and the two plants with no bed show 2

Partitioned Aggregates: Compare Each Row to Its Own Group

August 11, 2011
Pinal Dave
SQL Tips and Tricks
SQL Function, SQL Group By, SQL Scripts, SQL Server

Partitioned aggregates let every row see a number for its own group, such as how many plants share its garden bed. This post uses thirteen seedlings in four beds to show PARTITION BY with COUNT and AVG. It also shows the integer division trap that turns every share into 0, and how to get one row per group.

Read More
A detached ceramic lid beside an empty pale cup and a blue cup holding a stone on sage cloth.

FOR XML XSINIL: Emit Missing Fields as Explicit Nil Elements

August 10, 2011
Pinal Dave
SQL Tips and Tricks
SQL Function, SQL Scripts, SQL Server, SQL XML

FOR XML XSINIL can represent SQL NULL with an explicit element carrying the xsi:nil attribute. I compare that output with ordinary element omission. An empty string stays a separate input in both versions.

Read More
Two results for the same eight trip costs. SUM(Cost) returns one row, 1385.00. SUM(Cost) OVER ( ) returns all eight rows with 1385.00 beside each one

Aggregates With the OVER Clause: A Total Beside Every Row

August 10, 2011
Pinal Dave
SQL Tips and Tricks
SQL Function, SQL Group By, SQL Scripts, SQL Server

Aggregates with the OVER clause put a total, an average or a minimum next to every row, so the rows do not collapse into one. This post uses eight trip costs to show the error a plain SUM gives, the fix, and the percent of the total for each row. It also shows how WHERE changes the total and how to filter on the result.

Read More
Gouache painting of a stack of blank cards and an open document tray, representing a set of rows.

SQL Server: Use INSERT INTO SELECT Instead of a Cursor

August 10, 2011
Pinal Dave
SQL Tips and Tricks
Best Practices, SQL Scripts

Replace a row-by-row server inventory copy with INSERT INTO SELECT and test the results, including a loop that fails when IDs have gaps.

Read More
One brass key blank fits a precise profile jig while a similar blank with a different ridge rests outside

LIKE Character Ranges: [a-f], [^0-9] and Collation Traps

August 9, 2011
Pinal Dave
SQL Tips and Tricks
SQL Collation, SQL NULL, SQL String, Unicode

A bracket range sounds simple until the collation decides what sits between a and f. Test it once so a bad code never reaches your table.

Read More
Five rows of ID boxes. At the start the IDs are 1 to 5. After deleting 4 and 5, a new row gets ID 6. After restoring 4 and 5 with IDENTITY_INSERT on, the next new row gets ID 7

Overriding Identity Fields: Put a Deleted Row Back With Its Old ID

August 9, 2011
Pinal Dave
SQL Tips and Tricks
SQL Constraint and Keys, SQL Identity, SQL Scripts, SQL Server

Overriding identity fields is rare, but restoring a deleted row with its old ID needs it. This post uses IDENTITY_INSERT to put two workshops back with their old numbers. It shows the five errors you will meet, what happens to the next automatic number, and why gaps in an identity column are usually fine.

Read More
Three tables side by side. Orders has four rows and Plants has five rows. The OrderItems table between them has seven rows, and each row pairs one order number with one plant number and a quantity

Many-to-Many Bridge Table in SQL Server: Model and Query It

August 8, 2011
Pinal Dave
SQL Tips and Tricks
Normalization, SQL Constraint and Keys, SQL Joins, SQL Scripts

A many-to-many bridge table links two tables that each have many partners in the other, such as orders and plants. This post builds one with a composite key and two foreign keys, then joins it three ways. It also shows what the keys block and why an inner join hides plants nobody ordered.

Read More
Timeline of one update that is rolled back after 12 seconds and five readers that start after 3 seconds. The default reader waits 9 seconds and sees 24.00. READUNCOMMITTED and NOLOCK see 240.00 at once. READPAST skips the locked row. NOWAIT fails with Msg 1222. With row versioning on, the default reader sees 24.00 at once

Dirty Records and Table Hints: Wait, Read Dirty, Skip or Fail

August 7, 2011
Pinal Dave
SQL Tips and Tricks
SQL Lock, SQL Scripts, SQL Transactions, Transaction Isolation

Dirty records and table hints decide what a SELECT does while another session has an uncommitted change. This post runs one update that rolls back and five readers against it: the default, READUNCOMMITTED, NOLOCK, READPAST and NOWAIT. It also shows how a database option changes the default.

Read More
Two panels comparing separate INSERT statements with one multi-row INSERT when book 4 is a duplicate key. Separate statements save book 11 and leave 9 books. The multi-row INSERT saves nothing and leaves 8 books

Multi-Row INSERT in SQL Server: One Statement, All or Nothing

August 6, 2011
Pinal Dave
SQL Tips and Tricks
SQL Scripts, SQL Server, SQL Table Operation, SQL Transactions

A multi-row INSERT adds several rows with one statement, using a list of value groups separated by commas. This post compares it with separate INSERT statements on a small table of books. It shows that one bad row undoes the whole statement, and it covers the errors and the 1,000 row limit.

Read More
Result of a LEFT JOIN from six plants to their order lines. Orchid, Bamboo and Mint show NULL in the LineID column, and the filter LineID IS NULL keeps only those three

Unmatched Records Query: Four Ways to Find Rows With No Match

August 5, 2011
Pinal Dave
SQL Tips and Tricks
SQL Joins, SQL NULL, SQL Scripts, SQL Sub Query

An unmatched records query finds the rows in one table that have no partner in another, such as plants that never sold. This post runs four versions on a small table: LEFT JOIN with a NULL test, NOT EXISTS, NOT IN and EXCEPT. It also shows the NULL trap that makes NOT IN return nothing.

Read More
Previous 1 … 408 409 410 411 412 413 414 … 593 Next
  • Books
  • Testimonials
  • Privacy Policy

© 2006 – 2026 All rights reserved. pinal @ SQLAuthority.com