Understanding of difference between MariaDB and SQL Server for a query - sql

I am using MS SQL Server Version 2008 and MariaDB 10.5.
My table and data :
Following is the query i am running on both the databases.
select distinct distributor from market order by city
Found this query is failing in MS SQL Server database with error
SQL Error [145] [S0001]: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Understood the reason why it failing
Reason for - ORDER BY items must appear in the select list if SELECT DISTINCT is specified
when i ran the same query run with MariaDB it ran successful without any error and gave me below output.
So i have doubt why MariaDB is behave different here? ideally it should fail right?
Thanks in advance.

Please try this query
Select distinct col from (
Select col from table order by col
)

Related

Microsoft SQL Server and Oracle SQL Developer

I linked an Oracle Database to my SQL Server in Microsoft SQL Server Management Studio 18. Server Objects -> Linked Servers.
I have a SQL Statement that when I run on the Oracle Developer Tool/Platform it returns the information as expected. But when I run the exact same query on the SQL Server it returns the incorrect results (The actual values in the rows and columns do not match).
What I know.
The table I am query in lives in the Oracle Database.
I can get the same/matching results on the Oracle Developer and SQL Server if I exclude in my WHERE statement anything involving a DATE.
Any thoughts?
The example of the query below.
Works on Oracle Developer but not on MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE('01/03/2020', 'DD/MM/YYYY') AND TO_DATE('10/12/2020','DD/MM/YYYY');
The example of the query below.
Works on both Oracle Developer and MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and BATCHID = 'ThisBAtchID';
You cannot use ORACLE specific functions like TO_DATE in SQL Server calls. You have to execute them remotely using OPENQUERY. OPENQUERY in MSDN
SELECT * FROM OPENQUERY (OracleSvr, 'SELECT * FROM TABLE1
WHERE status = ''Deviation'' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE(''01/03/2020'', ''DD/MM/YYYY'') AND TO_DATE(''10/12/2020'',''DD/MM/YYYY'');');

sql server shortcut for select statement writing

Many times a day I have to write similar queries to get single record:
select t.*
from some_table t
where t.Id = 123456
maybe there is some shortcuts for retrieving single record? Like entering id, table and SQL server generates rest code automatically
In Sql Server Go to
Tools-> Options-> Environments->Keyboard
You will get shortcuts, there you can define your own as well as get the standards.
you can set a short cut for a fully executable query like
select * from table where id =20
but not like below
select * from

How to use GROUP BY for multiple columns in SQL server

I am working on existing project which is developed on Laravel + Mysql. Now, I have migrated my database from Mysql to Sql server. So all the queries written in project using Mysql syntax.
Now, I have an query given below:
SELECT table_1.*, table_3.date as sem1date
FROM table_1, table_2, table_3
WHERE table_3.ID=table_2.DID AND block_datelu.BID=table_1.final_exam_date AND table_1.centreid=1234 AND table_1.course=1
GROUP BY table_1.start_date, table_1.end_date
When I am converting above query into sql server syntax, then sql server giving me error msg "Column 'table_1.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
Can anyone tell me how can I use group by for multiple columns having joins?

Sub-Queries in Sybase SQL

We have an application which indexes data using user-written SQL statements. We place those statements within parenthesis so we can limit that query to a certain criteria. For example:
select * from (select F_Name from table_1)q where ID > 25
Though we have discovered that this format does not function using a Sybase database. Reporting a syntax error around the parenthesis. I've tried playing around on a test instance but haven't been able to find a way to achieve this result. I'm not directly involved in the development and my SQL knowledge is limited. I'm assuming the 'q' is to give the subresult an alias for the application to use.
Does Sybase have a specific syntax? If so, how could this query be adapted for it?
Thanks in advance.
Sybase ASE is case sensitive w.r.t. all identifiers and the query shall work:
as per #HannoBinder query :
select id from ... is not the same as select ID from... so make sure of the case.
Also make sure that the column ID is returned by the Q query in order to be used in where clause .
If the table and column names are in Upper case the following query shall work:
select * from (select F_NAME, ID from TABLE_1) Q where ID > 25

SQL Server 2008 JOIN with Oracle 11g

I have successfully linked an Oracle 11g database to SQL Server 2008. I can run a simple query in SQL Server which displays Oracle data:
SELECT PRODUCT_CODE
FROM [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE
This does exactly what I would expect it to do.
The problem comes when I try to do a simple join:
SELECT ProductName, [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE.PRODUCT_NAME
FROM SqlServer_table
INNER JOIN [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE
ON SqlServer_table.Product_ID=[ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE.PRODUCT_CODE
This causes a 'The multi-part identifier could not be bound' error on the Oracle part of the JOIN statement. I know from research that the syntax has to be exact, and I think I've tried almost every permutation. Perhaps there's something in the SQL Server settings/registry...
Following #shiva's helpful suggestion I reformatted the query with aliases. This threw a server collation mismatch error, which proved much more illuminating than the 'The multi-part identifier' message I had been getting earlier. It turns out the joined columns had different collations (SQL_Latin1_General_CP1_CI_AS vs Latin1_General_CI_AS). Adding a simple COLLATE to the end of the JOIN AS part of the query sorted this out.
Have you tried alias-ing the table names? Like so
SELECT SqlSvr.ProductName AS Sql_ProductName
, Orcl.PRODUCT_NAME AS Orcl_ProductName
FROM SqlServer_table AS SqlSvr
INNER JOIN [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE AS Orcl
ON SqlSvr.Product_ID = Orcl.PRODUCT_CODE