How can we use EXISTS clause in sequelize such that it fires a query in the following manner ?
IF EXISTS(SELECT FIRSTNAME FROM EMPLOYEES WHERE FIRSTNAME LIKE 'JOHN%')
This might be done by inserting the same query in the .query() method, but is there any way we can use EXISTS Clause in the findOne() method ?.
You can construct a sequelize literal like below -
where: {
[op.and]: [Sequelize.literal('exists ( SELECT FIRSTNAME FROM `EMPLOYEES` WHERE `EMPLOYEES`.`FIRSTNAME` LIKE 'JOHN%' )')]
},
Hope it helps!
Related
I need to query a SQLite database for some entries containing a field the value of which can be one of a defined list: 'Token1', 'Token2', ..., 'TokenN' - potentially a long one.
The straightforward SELECT statement would be something like
SELECT * FROM `my_table` WHERE `token` = 'Token1' OR `token` = 'Token2' OR ...
- far from elegant. I wonder, is there a better way to formulate the statement?
You can use IN Clause
SELECT * FROM `my_table`
WHERE `token` in ('Token1','Token2', 'Token3', ....);
Use the wildcard character %%.
SELECT * FROM `my_table` WHERE `token` like '%Token%'
If your defined list is or can be placed in a table, you can do the following:
SELECT
*
FROM [employeeName] Where dept In (Select dept From #tbl)
If i use a table containing at least 2 columns like this :
[User]
[FirstName]|[LastName]
Robert-Dupont
Louis-Dupont
Georges-Andre
Assuming that there is a huge amount of data and that there is much more unique result in 'FirstName' than in 'LastName', which query is the fastest :
Query 1 :
SELECT * USER WHERE LastName = 'Dupont' AND FirstName = 'Robert'
Query 2:
SELECT * USER WHERE FirstName = 'Robert' AND LastName = 'Dupont'
Is there a difference ?
Thanks for your help !
The order of the comparisons does not matter. SQL Server should optimize the WHERE clause for the data.
However, if you care about performance, you should use an index, either user(firstname, lastname) or user(lastname, firstname). The order of comparisons is not the right thing to be thinking about.
an index of below form
(firstname,lastname)
Can help in both the queries..
firstname='' and lastname=''
or
lastname='' and fastname=''
so your both queries are faster,but when you issue query of below form ,with same index
firstname>='' and lastname=''
Firstname uses index and SQLServer can't use index on second column ,but it uses residual lookup to filter out values
Both query are same faster. Because 'where' clause includes conditions. That is not important unique columns .
it's exactly the same when both are on the same level. but this is definitely better:
SELECT *
FROM (SELECT *
FROM USER
WHERE LastName = 'Dupont')
WHERE FirstName = 'Robert'
EDIT
Going back to Relational Algebra, it's better to filter any results as far down the tree as possible. Assuming that last names are rarer than first names, you 1st filter the 'Duponts' and then look for the 'Roberts' in the result set.
I am in a situation where I need to return results if some conditions on the string/character are met.
For example: to return only the names that contain 'F' character from the Person table.
How to create an SQL query based on such conditions? Is there any link to a documentation that explains how can SQL perform such queries?
Thanks in advance
The most basic approach is to use LIKE operator:
-- name starts with 'F'
SELECT * FROM person WHERE name LIKE 'F%'
-- name contains 'F'
SELECT * FROM person WHERE name LIKE '%F%'
(% is a wildcard)
Most RDBMS offer string operations which are able to perform that required task in one way or the other.
In MySQL you might use INSTR:
SELECT *
FROM yourtable
WHERE INSTR(Person, 'F') > 0;
In Oracle, this can be done, too.
In PostgreSQL, you can use STRPOS:
SELECT *
FROM yourtable
WHERE strpos(Person, 'F') > 0;
Usually there are several approaches to solve this, many would choose the LIKE operator. For more details, please refer to the documentation of the RDBMS of your choice.
Update
As requested by the questioner a few words about the LIKE operator, which are used not only in MySQL or Oracle, but in other RDBMS, too.
The use of LIKE will in some cases make your RDBMS try to use an index, it usually does not not try to do so if you use a string functions.
Example:
SELECT *
FROM yourtable
WHERE Person LIKE 'F%';
The query may look like this:
SELECT * FROM Person WHERE FirstName LIKE '%F%' OR LastName LIKE '%F%'
Is there anyway to use wildcards in a clause similar to a "in", like this
select * from table where columnx xxxxxxx ('%a%','%b%')?
I know I can do:
select * from table where (columnx like '%a%' or columnx like '%b%')
But I'm looking for an alternative to make the querystring shorter.
BTW: I'm not able to register any custom functions, nor temp tables, it should be a native DB2 function.
I found this similar answer for oracle and SQLServer:
Is there a combination of "LIKE" and "IN" in SQL?
There's no native regular expression support in "pureSQL" for DB2, you can either create your own as in:
http://www.ibm.com/developerworks/data/library/techarticle/0301stolze/0301stolze.html
or use pureXML as in: http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.xml.doc/doc/xqrfnmat.html
Example:
where xmlcast(xmlquery('fn:matches(\$TEXT,''^[A-Za-z 0-9]*$'')') as integer) = 0
Yet another variant that may be shorter:
select t.*
from table t
join ( values '%a%', '%b%' ) u (columnx)
on t.columnx like u.columnx
am struggling in ilike operation in pgSQL
select * from gtab47 where areaname ilike '(select place from gcompany where companyid=3)%'
how to correct it ?
There are a couple of things wrong here.
Subqueries aren't quoted and you must use || to concatenate strings, so you probably want to write something like:
select *
from gtab47
where areaname ilike (select place || '%' from gcompany where companyid=3)
Note that this query is incorrect unless companyid is UNIQUE or a PRIMARY KEY, since subqueries used in expressions must return at most one row.
If it's not unique or a primary key, you'll need to rephrase the query to use a join, or to use LIKE ANY using a subquery with array_agg.