FROM keyword not found in oracle 11g - oracle11g

I am using Oracle 11g client.
SELECT DISTINCT TOP 3 Salary FROM employees ORDER BY Salary DESC;
Giving error
FROM keyword not found where expected.
Can anyone please help?

In Oracle11g i guess you need to use DISTINCT like this:
SELECT * FROM (SELECT DISTINCT Salary FROM employees ORDER BY Salary DESC) WHERE rownum < 4
Here rownum works like TOP provided with the condition < 4 or you can pass rownum <= 3

Related

TeraData equivalence for limit offset SQL - PAGED

In MySQL and other engines I use a statement of the type:
SELECT reference FROM table WHERE field = 'iphone' ORDER BY reference LIMIT 2 OFFSET 0;
But in TeraData I can't find an equivalency to perform a paginated query.
I appreciate your ideas ;)
I found this options
1.
SELECT RANK(reference) as row_num, reference
FROM table
WHERE field = 'iphone'
ORDER BY 1
QUALIFY row_num BETWEEN 2 and 4;
SELECT RANK() OVER (ORDER BY reference) as row_num, reference
FROM table
WHERE field = 'iphone'
QUALIFY row_num BETWEEN 2 and 4;
But I'm not sure which one is the best

INSERT OR IGNORE conversion sqlite to mariadb

I'm trying to convert my SQlite code to Mariadb but i'm stuck with a query for a crossed table between two another tables
The purpose of the table is to add ID from the other table but only if they don't exists
I have two tables
Table COMPUTERS
ID(primary) / NETBIOS(unique) / IP / SOFTWARE_STAT / COPY_STAT / AV_STAT
ex : 1 PC1 192.168.1.1 KO KO 0
Table SOFTWARES
ID(primary) / NAME(unique)
ex : 1 ADOBE
And the cross table
Table INSTALL
ID (primary)/ COMPUTER_ID / SOFTWARE_ID / FAIL
1 1 1 0
My SQLITE code below is working
INSERT OR IGNORE INTO INSTALL (COMPUTER_ID,SOFTWARE_ID)
SELECT
(SELECT ID FROM COMPUTERS WHERE IP = '192.168.1.1'),
(SELECT ID FROM SOFTWARES WHERE NOM = 'ADOBE')
WHERE NOT EXISTS
(SELECT
COMPUTER_ID,SOFTWARE_ID FROM INSTALL
WHERE
COMPUTER_ID = (SELECT ID FROM COMPUTERS WHERE IP = '192.168.1.1')
AND
SOFTWARE_ID = (SELECT ID FROM SOFTWARES WHERE NOM = 'ADOBE')
)
I've tried that
INSERT INTO INSTALL (COMPUTER_ID,SOFTWARE_ID)
SELECT
(SELECT RowID FROM COMPUTERS WHERE IP='192.168.1.1'),
(SELECT RowID FROM SOFTWARES WHERE NOM='ADOBE')
WHERE NOT EXISTS
(SELECT COMPUTER_ID,SOFTWARE_ID FROM INSTALL
WHERE
COMPUTER_ID = (SELECT RowID FROM COMPUTERS WHERE IP='192.168.1.1')
AND
SOFTWARE_ID = (SELECT RowID FROM SOFTWARES WHERE NOM='ADOBE'));
Without success
Someone have an idea ? Thanks in advance.
INSERT OR IGNORE --> INSERT IGNORE
However, you have a redundancy: (1) IGNORE, and (2) NOT EXISTS. That is, you both verify that it is not a dup and you ignore errors if it is.
Otherwise, the Sqlite code should work as is in MariaDB. What went wrong? Did you get an error?
MariaDB has no RowID.

BigQuery Window ORDER BY is not allowed if DISTINCT is specified

I'm investigating porting some bigquery legacy sql containing windowed distinct counts like this
count(distinct brand_id) over (partition by user_id order by order_placed_at range between 7 * 24 * 60 * 60 * 1000000 PRECEDING AND 1 PRECEDING) as last_7_day_buyer_brands
to Standard sql.... but I get this error....
Window ORDER BY is not allowed if DISTINCT is specified
For reference I've tried APPROX_COUNT_DISTINCT function with no luck.
Is there a better way to get this to work other than write the subqueries and group by's?
Most of the other queries have ported to standard sql with only minor changes.
Per documentation
OVER clause requirements:
PARTITION BY: Optional.
ORDER BY: Optional. Disallowed if DISTINCT is present.
window_frame_clause: Optional. Disallowed if DISTINCT is present.
note: above is 'highlighted' by me, not as in documentation
As you can see not only ORDER BY but even RANGE BETWEEN is not allowed when DISTINCT is used
I think, subquery is the way to go.
In case if you need direction for this, use below simple example
#standardSQL
SELECT
user_id,
order_placed_at,
brand_id,
(SELECT COUNT(DISTINCT brand)
FROM UNNEST(last_7_day_buyer_brands_with_dups) AS brand
) AS last_7_day_buyer_brands
FROM (
SELECT
user_id,
order_placed_at,
brand_id,
ARRAY_AGG(brand_id) OVER(
PARTITION BY user_id ORDER BY order_placed_at
RANGE BETWEEN 7 * 24 * 60 * 60 * 1000000 PRECEDING AND 1 PRECEDING
) AS last_7_day_buyer_brands_with_dups
FROM yourTable
)

Converting SQL Server to Oracle

In my project, I have a database in SQL which was working fine. But now I have to make the application support oracle db too.
Some limitations I found out was that in Oracle, there is no bit field and the table name cannot be greater than 30 char. Is there any other limitation that I need to keep in mind.
Any suggestion from past experience will be helpful.
If I recall correctly from my earlier Oracle days:
there's no IDENTITY column specification in Oracle (you need to use sequences instead)
you cannot simply return a SELECT (columns) from a stored procedure (you need to use REF CURSOR)
of course, all stored procs/funcs are different (Oracle's PL/SQL is not the same as T-SQL)
The SQL ISNULL counterpart in Oracle is NVL
select ISNULL(col, 0)...
select NVL(col, 0)...
You will also struggle if you attempt to select without a from in Oracle. Use dual:
select 'Hello' from DUAL
Bear in mind also, that in Oracle there is the distinction between PL/SQL (Procedural SQL) and pure SQL. They are two distinct and separate languages, that are commonly combined.
Varchar in Oracle Databases called
varchar2 is limited to 4000
characters
Oracles concept of temporary tables is different, they have a global redefined structure
by default sort order and string compare is case-sensitive
When you add a column to a select *
Select * from table_1 order by id;
you must prefix the * by the table_name or an alias
Select
(row_number() over (order by id)) rn,
t.*
from table_1 t
order by id;
Oracle doesn't distinguish between null and '' (empty string). For insert and update you ca use '', but to query you must use null
create table t1 (
id NUMBER(10),
val varchar2(20)
);
Insert into t1 values (1, '');
Insert into t1 values (2, null);
Select * from t1 where stringval = 0; -- correct but empty
Select * from t1 where stringval is null; -- returns both rows
ORACLE do not support TOP clause. Instead of TOP you can use ROWNUM.
SQL Server: TOP (Transact-SQL)
SELECT TOP 3 * FROM CUSTOMERS
ORACLE: ROWNUM Pseudocolumn
SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3

How can we fetch top 3 data value from database

There is three field in my data base id(primary key),name, salary
I want fetch top 3 salary from the database.
Use LIMIT to get top 3 after ordering, as such:
SELECT *
FROM myTable
ORDER BY salary DESC
LIMIT 3;
SELECT * FROM your_table ORDER BY id DESC;
SELECT [column(s)]
FROM [table]
ORDER BY [column(s)] [ASC, DESC];
For more information check here:
http://www.sqlite.org/lang_select.html
SQL has an ORDER BY clause that allows you do order the result set by any column/columns, ascending and descending.
For your particular question:
SELECT Id, Name
FROM myTable
ORDER BY Id DESC;
See this SO question (SQLite - sorting a table).

Resources