Skip to main content

New answers tagged

0 votes

Why does LEFT JOIN return fewer rows when I add a condition on the joined table in WHERE clause?

The answer is because null is treated as false-y in a WHERE clause. You can test this out by comparing: SELECT 1 WHERE true; -- vs SELECT 1 WHERE null; You have a null occurring in the WHERE clause ...
Dunes's user avatar
  • 42.4k
0 votes

SQL Select WHERE duplicate in one column x AND column y != z

None of the above will work if you can have a unique column in table to identify which one to keep. In case of true duplicate, you will need to extract duplicate records to a temp table delete all ...
Uday Barua's user avatar
Best practices
1 vote
0 replies
0 views

Passing columns as function arguments (paired)

Thanks! That's really the answer.
Kuraga's user avatar
  • 435
0 votes

Count based on condition in SQL Server

You can use COUNT() and CASE. Simply don't define a value for your ELSE condition since NULL values are skipped when using COUNT(). COUNT(CASE WHEN <condition> THEN 1 END) In your specific case:...
Stevoisiak's user avatar
0 votes

Current date and current date in past years

Another version of selecting a current date: SELECT Date, Content FROM MYDATABASE WHERE Date LIKE '%-07-15'; More details in SQLite documentation.
Gabriel Bap's user avatar
Best practices
1 vote
0 replies
0 views

Passing columns as function arguments (paired)

You forgot to add you function signature - without that, "paired" arguments can mean a number of things. It helps to underline you're only looking for alternate ways to invoke a function, ...
Zegarek's user avatar
  • 31k
4 votes

Return data between certain Hour values

The column TrxnDate in your table is a misnomer and doesn't contain dates, but datetimes / timestamps. Now you want to select all rows falling in a particular date range and additionally in a ...
Thorsten Kettner's user avatar
4 votes

Return data between certain Hour values

You can use a join to a list of date values, plus some calculation to add the hours. declare @startHour int = 0, @endHour int = 6; select TrxnDate, UserName, UserType from (values (CAST('...
Charlieface's user avatar
  • 80.4k
0 votes

How to map native sql results to oneToMany field with SqlResultSetMapping in JPA

You can do it by using Hibernate API. // given EntityManager em instance Session s = em.unwrap(Session.class); NativeQuery<Building> q = s.createNativeQuery(""" SELECT {b....
Jakub Ch.'s user avatar
  • 3,777
Best practices
0 votes
0 replies
0 views

Passing columns as function arguments (paired)

Thanks! But the task is to not change the function.
Kuraga's user avatar
  • 435
Best practices
2 votes
0 replies
0 views

Passing columns as function arguments (paired)

Instead of scanning the table t twice, it might be better to collect an array of pairs with a single subquery. Here is a complete example. We need a composite data type to represent a pair: CREATE ...
Laurenz Albe's user avatar
1 vote

How to check owner of delta table in Databricks

In 2026, the official way to see an object's owner is : DESCRIBE <securable-type> EXTENDED <catalog>.<schema>.<securable-name>; and with no special privileges: Permissions ...
Jose R's user avatar
  • 836
1 vote
Accepted

Recursive CTE to return item where a descending table and an ascending formula meet

The code below makes a couple of changes to your code. First, you need to enumerate the source data, so that you can process one row at a time. I used ROW_NUMBER for that. Second, the recursive branch'...
MatBailie's user avatar
  • 87.9k
0 votes

Firebase Database in SQL

Firebase (Firestore/Realtime Database) is NoSQL — there's no built-in SQL mode. But you can query your Firestore data with real SQL using fire_duck_ext, a DuckDB community extension that connects ...
user3440094's user avatar
7 votes

Duplicated data from a select

You are missing a join condition between schedules_tb.user_id and users_tb.id. Also this function could probably be a normal sql function rather than pgpsql, this will allow for inlining. You should ...
Charlieface's user avatar
  • 80.4k
0 votes

LEFT JOIN Duplicating Rows

This is an old request, but well, I thought I'd just add an answer, for future readers may benefit from this. Unfortunately, the request shows a mutilated query instead of the real one. Those who can ...
Thorsten Kettner's user avatar
2 votes

Recursive CTE to return item where a descending table and an ascending formula meet

This doesn't actually need any kind of recursion, you just need a couple of window functions. ROW_NUMBER will count the rows, then we subtract one to get a zero-based start. We also need LEAD(1) so we ...
Charlieface's user avatar
  • 80.4k
Best practices
3 votes
0 replies
0 views

What is the best way to remember which function to use for which situation?

Think about how you would learn chords of a particular song on guitar. How would you do it? You'd find a guitar tab reference, and study it. Then you'd play it, probably not the whole song at first, ...
Bill Karwin's user avatar
Best practices
0 votes
0 replies
0 views

What is the best way to remember which function to use for which situation?

If you use something occasionally then it's impossible to remember all details. If you use something few times a day then brain can remember it automatically.
furas's user avatar
  • 149k
Best practices
3 votes
0 replies
0 views

What is the best way to remember which function to use for which situation?

Same. I've been using MySQL for 25 years, and I still refer to documentation.
Bill Karwin's user avatar
Advice
1 vote
0 replies
0 views

Starting out on GitHub to create a portfolio

One public repo per project, with a top level README.md thoroughly describing it. You can also have README.md files in directories. Link to other projects where there's some relation.
ysth's user avatar
  • 99.3k
Best practices
0 votes
0 replies
0 views

What is the best way to remember which function to use for which situation?

In addition to checking documentation, I like to create a Markdown file with ways to solve problems, or code snippets. Creating a GitHub Gist works too.
Diode's user avatar
  • 36
Best practices
7 votes
0 replies
0 views

What is the best way to remember which function to use for which situation?

This is what documentation is for. I've been using MySQL for over a decade and I still consult the documentation occasionally to get the function names and argument order correct.
Barmar's user avatar
  • 790k
Advice
3 votes
0 replies
0 views

Starting out on GitHub to create a portfolio

GitHub just hosts code repositories. Step one is to have something to show. Maybe you're thinking of GitHub pages? It is meant to host documentation, but you can use a static site generator to create ...
Greg Burghardt's user avatar
0 votes

LEFT JOIN Duplicating Rows

My expectation is that the join would yield exactly as many rows as table1 without the join. The join would just bring in one more column (fieldX) This expectation is incorrect. Rows for table1 are ...
Joel Coehoorn's user avatar
Advice
1 vote
0 replies
0 views

Does flipping from/to dates within date index make it more selective for SQL Server

I would suggest to try it: create an index on (start_date, expiry_date) create one on (expiry_date, start_date) check which index gets used and when what happens, if you search for very young entries, ...
raffael's user avatar
  • 11
Best practices
0 votes
0 replies
0 views

RBAC: How to model per-user permission overrides?

I am planning to make my backend api query a user's privileges from the users_permissions only and check for is_granted ( role_permissions table are only for seeding the default permissions in ...
Corvus-OS's user avatar
3 votes

XML format from SQL data table

In this case you can use both a ROOT parameter as well as a column alias. SELECT id, name, (SELECT class_name FROM classes c WHERE c.student_id = s.id FOR XML PATH('...
Charlieface's user avatar
  • 80.4k
3 votes

XML format from SQL data table

Please try the following solution. You just need to add one more level to the XML hierarchy. XML is case-sensitive. Your T-SQL element names are not matching the desired output. For example: PATH('...
Yitzhak Khabinsky's user avatar
Best practices
0 votes
0 replies
0 views

RBAC: How to model per-user permission overrides?

How is this relevant to the question asked?
Dale K's user avatar
  • 28.3k
Best practices
0 votes
0 replies
0 views

RBAC: How to model per-user permission overrides?

Then what happens if the permissions assigned to a role are changed some time later?
Bill Karwin's user avatar
4 votes
Accepted

On Update Cascade not cascading in child table

my challenge and thinking is that whenever I make a change to either the receipt number, date or amount in my PT using the y_trans_code as my where statement this should affect the entire row in the ...
Charlieface's user avatar
  • 80.4k
Best practices
0 votes
0 replies
0 views

RBAC: How to model per-user permission overrides?

FYI, see the attached hyperlink for a more comprehensive and abstract explanation of the issue. https://en.wikipedia.org/wiki/Hilbert_scheme?wprov=sfla1
Ariel Liberty's user avatar
Best practices
0 votes
0 replies
0 views

RBAC: How to model per-user permission overrides?

Regarding your point no 1, I am thinking of querying a user's privileges from the users_permissions only and check for is_granted . ( role_permissions are only for seeding the default permissions in ...
Corvus-OS's user avatar
Best practices
1 vote
0 replies
0 views

RBAC: How to model per-user permission overrides?

Your design models per-user overrides, with caveats: A given user's privileges are the union of those granted by roles and those granted as overrides. To query the user's privileges, you'll have to ...
Bill Karwin's user avatar
0 votes

Unterminated dollar quote

Maybe my suggest will help someone. If you are using liquibase then you have to specify for sql script option splitStatements to disable split statement by semicolons. In your changelog.xml file it ...
Yan Pak's user avatar
  • 1,877
0 votes

Replace year in datetime date

I just had to solve a similar problem but my years where wildly distributed, ranging from 2030 to 4077, which I wanted to reset to 1930 and 1977 respectively while keeping day and month. So I ...
Damian Vogel's user avatar
  • 1,245
Advice
2 votes
0 replies
0 views

Does flipping from/to dates within date index make it more selective for SQL Server

One thing developers must keep in mind is that SQL is not a procedural language. It's a calculus: The engine will compile your query and, based on heuristics, try to plan the most efficient way to ...
jean's user avatar
  • 4,357
Advice
1 vote
0 replies
0 views

Does flipping from/to dates within date index make it more selective for SQL Server

If expiry_date is the more selective of the two dates, as you describe, then make it the first column in the index. The query, however, stays the same. It doesn't matter how you write the query, the ...
Thorsten Kettner's user avatar
Advice
1 vote
0 replies
0 views

Does flipping from/to dates within date index make it more selective for SQL Server

I'd expect DBMS engine to benefit from fast return for the case when expiry_date is checked first, for it's more likely to return false in case the period between start_date and expiry_date is ...
Sergey Tsypanov's user avatar
Advice
0 votes
0 replies
0 views

Does flipping from/to dates within date index make it more selective for SQL Server

For the case that the range itself is selective a technique that can be used to work around this is https://dba.stackexchange.com/questions/14894/optimizing-ip-range-search/14896#14896
Martin Smith's user avatar
2 votes
Accepted

Avoid quoted JSON output in SQLite

If you'd look at The json_array() function docs, in the examples you could see that it returns text with serialized JSON, not an object. So if we were to put it as table result you would get something ...
Michał Turczyn's user avatar
Advice
3 votes
0 replies
0 views

Does flipping from/to dates within date index make it more selective for SQL Server

(Edited: I had my answer backwards. Also added demo fiddle that compares index reads.) I've always considered the inability to directly support ranges as a shortcoming of SQL Server (and likely most ...
T N's user avatar
  • 10.9k
Best practices
0 votes
0 replies
0 views

RBAC: How to model per-user permission overrides?

I have updated the post @Sergey
Corvus-OS's user avatar
Best practices
0 votes
0 replies
0 views

RBAC: How to model per-user permission overrides?

I have updated the thread with more detail @Laurenz
Corvus-OS's user avatar
Advice
2 votes
0 replies
0 views

Editing SQL view to include description

I think you might be talking about an ALIAS? ...SELECT WOTEMPLATEID AS DESCRIPTION,.....
Chris Catignani's user avatar
Advice
2 votes
0 replies
0 views

Editing SQL view to include description

So whats stopping you? What help are you asking for? Just add it? And which RDBMS? Following is for SQL Server https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-view-transact-sql?view=sql-...
Dale K's user avatar
  • 28.3k
0 votes

SQL query at least one of something

Haven't seen this answer yet: subquery with LIMIT 1. SELECT any_userdata FROM user WHERE (SELECT anything FROM whatever WHERE rating > 10 LIMIT 1) IS NOT NULL
WoodrowShigeru's user avatar
Advice
0 votes
0 replies
0 views

Does flipping from/to dates within date index make it more selective for SQL Server

I don't understand "should I replace my original query with the one relying on fast return for expiry_date?". How would you write a query that relies on "a fast return for expiry_date&...
Thorsten Kettner's user avatar
Advice
1 vote
0 replies
0 views

Does flipping from/to dates within date index make it more selective for SQL Server

I am not a SQL Server developer, so I only can give general advice. An index will only be used when it makes sense to the DBMS. It won't use an index, if it expects to have to read half the table ...
Thorsten Kettner's user avatar

Top 50 recent answers are included