r/SQL • • 22h ago

SQL Server SQl e a teia de aranha mental

0 Upvotes

Bom dia

Estou a aprender SQL desde meio de Setembro em um curso que estou a tirar. Na verdade começamos com uma introdução a SQL e saltamos para diagramas, relacionamrntos e cardinalidades. Agora voltamos para SQL( select,from,join, where, group by, order by, coalesce, null,...) Sinto me super confuso. Basicamente entendo os conceitos mas na parte prático ao tentar reproduzir os mesmos, sinto um espaço em branco. Parece que preciso das tabelas em excel ao lado para conseguir decifrar a solução. Sei que tenho ainda muito que praticar ( e só assim vou chegar lá). Mas parece que é algo que nao consigo ter um fio condutor. Parece um bicho dificil de domar minimamente.


r/SQL • • 15h ago

Discussion How would you compare two huge tables when there is no primary key and duplicate rows matter?

7 Upvotes

I'm comparing a source table with a target table after a data migration.

Both tables have hundreds of millions of rows, no reliable primary key, and some columns contain NULL values. Duplicate rows are also possible.

A simple EXCEPT query doesn't reveal differences in duplicate counts, and hashing every row may introduce collision risks or performance issues.

For example:

Source: A,A,A,B,C
Target: A,A,B,B,C

Both tables contain the same distinct values, but the actual data is different.

How would you identify missing, extra, and changed records efficiently without a unique key?

Would you use grouped row comparisons, partitioning, hashes, or a completely different strategy? How would you make it scale to hundreds of millions of rows?


r/SQL • • 14h ago

Discussion Better Autocomplete than DataGrip, one familiar language youll never need to write in. For any database or file, as a TUI, with built-in AI round trips, query history... for all languages and Databases

Thumbnail
github.com
1 Upvotes

Hi,

I have been working on a project: A unified workspace for all datatypes/frameworks supported by "ibis" (sort of like sqlglot but includes dataframes too).

To name a few i use on a day to day basis: Parquet, CSV, DuckDB, Postgres, MS SQL, and SQLite/D1, polars, pandas.

This project was built because spinning up Jupyter just to view a Parquet file is overkill, modern CSV editors are either ancient or freemium traps, and standard SQL IDEs struggle with live schema IntelliSense and quick parameter binding, and practically ignore your history.

This project adds a layer to solve those issues and adds more functionality, such as the ability to make parameter driven functions to kick off canned queries (without actually writing the functions to your DB).

Bring your own DB, bring your own AI (optional, but i would definitely recommend), and load any file. (Currently very early. LOTS of work to be done ).

The goal of this is to keep us heavy vibe coders who appreciate TUI experience (opencodecli, pi, oh my pi) and use multiplexers like herdr inside what we love -- terminal user interfaces.

No more bloated Datagrip and DBeaver-- all in one simplified language with shortcuts. Same syntax for all languages.

Some examples:

### 1. Filter without the boilerplate

\from orders

\where status = 'paid' and amount > 100

→

SELECT * FROM orders WHERE status = 'paid' AND amount > 100;

### 2. Top 10 biggest orders

\from orders

\select id, amount

\order amount desc

\limit 10

→

SELECT id, amount
FROM orders
ORDER BY amount DESC
LIMIT 10;

### 3. Automatic grouping — select a field and an aggregate. Done.

\from orders

\select customer_id, sum(amount) as revenue

→

SELECT customer_id, SUM(amount) AS revenue

FROM orders

GROUP BY customer_id;

No \group required. D8R derives the grouping from your selection.

### 4. Multiple dimensions, multiple aggregates — still auto-grouped

\from orders

\select region, status, count(*) as orders, sum(amount) as revenue, avg(amount) as average_order

→

SELECT region, status,
COUNT(*) AS orders,
SUM(amount) AS revenue,
AVG(amount) AS average_order
FROM orders
GROUP BY region, status;

### 5. Join shorthand when both tables share a key

\from orders
\join customers on customer_id
\select orders.id, customers.name, orders.amount

→

SELECT orders.id, customers.name, orders.amount
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id;

### 6. Join + aggregate + auto-group + ranking

\from orders o

\join customers c on o.customer_id = c.id

\select c.region, sum(o.amount) as revenue

\order revenue desc

\limit 5

→

SELECT c.region, SUM(o.amount) AS revenue

FROM orders AS o

JOIN customers AS c ON o.customer_id = c.id

GROUP BY c.region

ORDER BY revenue DESC

LIMIT 5;

### 7. Filter rows AND aggregate results with the same \where

\from orders

\select customer_id, sum(amount) as revenue

\where status = 'paid' and revenue > 1000

→

SELECT customer_id, SUM(amount) AS revenue
FROM orders
WHERE status = 'paid'
GROUP BY customer_id
HAVING SUM(amount) > 1000;

D8R separates the input filter from the aggregate-result filter.

### 8. Different filters inside different aggregates

\from orders

\select customer_id, sum(amount \where status = 'paid') as paid_total, count(* \where status = 'pending') as pending_orders

→

SELECT customer_id,

SUM(amount) FILTER (WHERE status = 'paid') AS paid_total,

COUNT(*) FILTER (WHERE status = 'pending') AS pending_orders

FROM orders

GROUP BY customer_id;

One query, independently filtered metrics, automatic grouping.

### 9. Calculate directly on aggregates

\from orders

\select customer_id, sum(amount_cents) / 100 as revenue_dollars

→

SELECT customer_id,
SUM(amount_cents) / 100.0 AS revenue_dollars
FROM orders
GROUP BY customer_id;

Numeric conversion, not formatting—and integer inputs don’t truncate the fraction.


r/SQL • • 17h ago

Discussion what is Strata sql?

0 Upvotes

Today I accidentally saw "Strata SQL" in a list of extensions for VS Code. It's a somewhat dubious website, but the stated functionality is intriguing. Judging by the browser demo, it's not just a visual DDL editor, it's a full-fledged utility for data documentation and reporting.
Does anyone know what this is? The project looks pretty polished, but it still looks suspicious.


r/SQL • • 8h ago

Discussion Before writing SQL in an interview, what would you clarify?

0 Upvotes

Suppose an interviewer asks you something like "find the top three products in each category by sales last quarter." Before you write the query, what would you clarify?

For me, I’d want to know what “sales” means -  units, revenue, or number of orders Then, I'd want to know things like how ties should work, which dates count, and what one row in the output should represent.

I’ve been thinking about how SQL interviews test more than whether you can produce a query. They also give you a chance to show how you frame the problem, explain your choices, and check your work.

So, I put together a guide on what to expect and how to prepare: What to Expect in a SQL Coding Interview

If you’ve recently done a SQL interview, what’s one thing you wish you’d known or asked beforehand?


r/SQL • • 19h ago

SQL Server Statistics on columns of a table

4 Upvotes

Limitations of table-valued parameters:

SQL Server does not maintain statistics on columns of table-valued parameters.

statistics of what ? why those statistics even exist for non table-valued parameters ?