IN or LIKE ANY or? - teradata

in terms of performance, whats the best choice in this case?
select *
from db.table
where username in ('john', 'kevin');
Or
select *
from db.table
where username like any ('john', 'kevin');
Or maybe there is a best way for performance to do this.
Thanks!

Of course IN.
Additionaly LIKE might not return the same result as the comparison rules differ, LIKE doesn't ignore trailing spaces, e.g.
'john' = 'john ' -- results in TRUE
'john' like 'john ' -- results in FALSE

Related

SQL Syntax Query

Can anyone advise where i'm going wrong with the syntax here please?
I have this query which returns 26 rows
SELECT firstname, lastname
FROM author
WHERE authorid IN (
SELECT authorid
FROM written_by
JOIN book ON written_by.bookdescid = book.bookdescid
WHERE UPPER (book.title) LIKE UPPER ('%electric%')
AND NOT UPPER (written_by.role) LIKE UPPER('%translator%'));
This appears to be correct and the reason I am using UPPER and ('%example%') like this is for variations in capitalization.
However, when I try and add into the query to also pick up where the book subtitle also includes 'electric' I am somehow getting the syntax wrong as it returns less rows? If anything it should be the same or more... I've tried a few variations but this one below is what i thought would work..
SELECT firstname, lastname
FROM author
WHERE authorid IN (
SELECT authorid
FROM written_by
JOIN book ON written_by.bookdescid = book.bookdescid
WHERE UPPER (book.title) OR (book.subtitle) LIKE UPPER ('%electric%')
AND NOT UPPER (written_by.role) LIKE UPPER('%translator%'));
You must use separate expressions with the operator LIKE:
WHERE ((UPPER(book.title) LIKE '%ELECTRIC%') OR (UPPER(book.subtitle) LIKE '%ELECTRIC%'))
AND (UPPER(written_by.role) NOT LIKE '%TRANSLATOR%');
Also use UPPER() only for the columns and provide the string literals in uppercase.

REGEXP_SUBSTR return all matches (mariaDB)

I need to get all the matches of a regular expression in a text field in a MariaDB table. As far as I know REGEXP_SUBSTR is the way to go to get the value of the match of a regular expression in a text field, but it always returns after the first match and I would like to get all matches.
Is there any way to do this in MariaDB?
An example of the content of the text field would be:
#Generation {
// 1
True =>
`CP?:24658` <= `CPV?:24658=57186`;
//`CP?23432:24658` <= `CPV?:24658=57186`
// 2
`CP?:24658` <> `CPV?:24658=57178` =>
`CP?:24656` <> `CPV?:24656=57169`;
And the select expression that I'm using right now is:
select REGEXP_SUBSTR(textfield,'CP\\?(?:\\d*:)*24658') as my_match
from table
where id = 1243;
Which at the moment returns just the first match:
CP?:24658
And I would like it to return all matches:
CP?:24658
CP?23432:24658
CP?:24658
Use just REGEXP to find the interesting rows. Put those into a temp table
Repeatedly process the temp table -- but remove the SUBSTR as you use it.
What will you be doing with each substr? Maybe that will help us devise a better approach.

Postgresql SQL: How check boolean field with null and True,False Value?

In my database table I am having one boolean column. which have some transaction with will False, True and Null.
These are the cases I have tried:
Case:1
select * from table_name where
boolean_column is null;
works well. Give the result with all transaction having null value for that column.
Case:2
select *from table_name where boolean_column = False;
Works well. Gives result with all the transaction having False value for that column.
Case:3
This is requirement which does not works. I want all the transaction having value False and Null.
I have tried these.
i) select *from table_name where boolean_column is False or Null;
Which only gives the result for False it does not shows null records.
ii) select *from table_name where boolean_column is Null or False;
*Which only gives the result for null it does not shows records with False value. *
iii) select *from table_name where boolean_column is Null or boolean_column = False;
This is simply display all the transaction does not applied any condition at all.
How can this issue be resolved?
There are 3 states for boolean in PG: true, false and unknown (null). Explained here: Postgres boolean datatype
Therefore you need only query for NOT TRUE:
SELECT * from table_name WHERE boolean_column IS NOT TRUE;
I'm not expert enough in the inner workings of Postgres to know why your query with the double condition in the WHERE clause be not working. But one way to get around this would be to use a UNION of the two queries which you know do work:
SELECT * FROM table_name WHERE boolean_column IS NULL
UNION
SELECT * FROM table_name WHERE boolean_column = FALSE
You could also try using COALESCE:
SELECT * FROM table_name WHERE COALESCE(boolean_column, FALSE) = FALSE
This second query will replace all NULL values with FALSE and then compare against FALSE in the WHERE condition.
On PostgreSQL you can use:
SELECT * FROM table_name WHERE (boolean_column IS NULL OR NOT boolean_column)
select *from table_name where boolean_column is False or Null;
Is interpreted as "( boolean_column is False ) or (null)".
It returns only rows where boolean_column is False as the second condition is always false.
select *from table_name where boolean_column is Null or False;
Same reason. Interpreted as "(boolean_column is Null) or (False)"
select *from table_name where boolean_column is Null or boolean_column = False;
This one is valid and returns 2 rows: false and null.
I just created the table to confirm. You might have typoed somewhere.
Resurrecting this to post the DISTINCT FROM option, which has been around since Postgres 8. The approach is similar to Brad Dre's answer. In your case, your select would be something like
SELECT *
FROM table_name
WHERE boolean_column IS DISTINCT FROM TRUE
#Brad Dre's answer is the least code and same efficiency as this one, but potentially harder to understand for future developers. Chris's third and Dmitry's answers already covered this, but I wanted to add an answer with a bit more background.
In the OP's words:
"I want all the transactions having value False and Null."
Re-worded in almost-sql:
"I want all the transactions where the value is False or the value is Null."
Re-worded into SQL:
SELECT *
FROM transactions
WHERE
value IS FALSE
OR value IS NULL;
Often writing the code to be as close as natural language as possible will give future developers hints on the intention of the initial developer. The issue with #Brad Dre's anwser is a future developer might not think of the NULL case in Brad's code, or might have thought Brad forgot about it.

Using prepared statements and full-text-search in SQLite

I'm using the SQLite C interface to write an application. Since I like security, I'm using prepared statements to query the database. In one such query, I'm selecting rows from a virtual database using the MATCH keyword for full-text-searching. Here's an example:
SELECT * FROM Emails
WHERE ( Subject LIKE ?001 OR ?001 IS NULL )
AND ( Author LIKE ?002 OR ?002 IS NULL )
AND ( Body MATCH ?003 OR ?003 IS NULL )
This allows the user to enter any terms (Subject, Author, or Body) individually or in any combination to do a search. Any term that isn't entered, I'll bind NULL to that parameter. The problem with that statement is that you can't use the OR keyword with the MATCH keyword. I'm looking for a statement I can use with the MATCH keyword to return all rows if not searching in the Body column. Is there such a statement?
I suggest the following:
SELECT * FROM emails
WHERE ...
AND ( CASE (SELECT COUNT(*) FROM emails WHERE body MATCH ?003)
WHEN 0 THEN 1
ELSE body MATCH ?003
END )
I ended up modifying the SQL statement at runtime to replace MATCH with LIKE '%'. Not very elegant, but it works for now.

SQL SERVER CASE STATEMENT CLARIFICATION

I have to execute a statement like ( i need and keyword along with when ).
select
'Is Allowed'= case A.Column
when
A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else
'No' end from TableName
I am getting syntax error,how to rewrite it without affecting the condition.
Try:
select case when A.Column='XXXX' and Isnull(A.AnotherColumn,'')<>'' then 'Yes'
else 'No' end as 'Is Allowed'
from TableName
SELECT
CASE A.Column
WHEN 'Is Allowed THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
ELSE 'Other'
END
Is the general way of making a CASE (which is your question). However, your query/logic looks a bit convoluted. More detailed answer / query is possible, but would perhaps use more statements/ nested CASE.
Have a closer look at CASE (Transact-SQL)
SELECT CASE
WHEN some boolean expression
THEN value
ELSE default value
END
or
SELECT CASE value to check
WHEN vlue to check agains
THEN value
ELSE default value
END

Resources