Create table - SQL Oracle - sql

I need to create table, which should I call Others. I want only employeers who names which having names start with any other letter, but not K
I wrote sthg like this:
CREATE TABLE others AS select * from employees WHERE last_name no like 'K%';
I found sthg like this idea but it doesn't work
I'm receiving errror about syntax. Can you help me?
The second question: there is any other way to write it?

Try This
CREATE TABLE others AS (SELECT *
FROM employees
WHERE last_name NOT LIKE 'K%');

As #jarlh said in his comment, a view would serve the same purpose, but the data would only be stored once instead of twice, thus saving disk space. You could define the view as
CREATE OR REPLACE VIEW OTHERS AS
SELECT *
FROM EMPLOYEES
WHERE LAST_NAME NOT LIKE 'K%';
Best of luck.

I would recommend using just string functions. Here are two ways:
WHERE SUBSTR(last_name, 1, 1) <> 'K'
or:
WHERE last_name < 'K' or last_name >= 'L'
Although you can use LIKE or REGEXP_LIKE() for this, I like this simpler approaches.

Related

Database query with two fields in the same table. Don't consider if any of the values are empty

The problem is I have a table with 4 columns. I have two search boxes.
Fields
FName
LName
Age
School
Text boxes
FName
School
If the user has inserted two values I want to get the intersect using both values. If only one value is present I want to have data using that value. I thought of not handling this in the application but with a stored procedure.
I thought of using IF ELSE in the stored procedure or having sub queries. But not a solid solution. I need some guidance to think of a possible way. Thank you in advance.
Here is what I have tried. This is just the query I need to embed this in a stored procedure
SELECT * FROM STUDENT WHERE FNname like '%TestFName%'
INTERSECT SELECT * FROM STUDENT WHERE School LIKE '%TestSchool';
If the 'TestSchool" becomes null it takes all the records which full fill the first query.
If both values are missing it returns the whole table.
If both values are there it returns the specific data tuples.
Pretty sure it is as simple as this.
SELECT *
FROM STUDENT
WHERE FNname like '%TestFName%'
AND School LIKE '%TestSchool%';
If use 'AND' rather than 'OR' it meets all the given conditions
SELECT *
FROM YourTable
WHERE (FName = #Fname OR #Fname = '')
AND (School = #School OR #School ='')
Here is the answer I came up for my own problem.
SELECT * FROM STUDENT WHERE FNname like '%TestFName%'
INTERSECT SELECT * FROM STUDENT WHERE School LIKE '%TestSchool';
In here if the 'TestFName" becomes null it takes all the records which full fill the second query.
If the 'TestSchool" becomes null it takes all the records which full fill the first query.
If both values are missing it returns the whole table.
If both values are there it returns the specific data tuples.
Thank you. If there is a better way than this, please enlighten us.
You can use this :
SELECT * FROM STUDENT WHERE isnull(FNname, '') like '%' + isnull(#FNname, '') + '%'
and isnull(School, '') like '%' + isnull(#School, '') + '%'
Use the parameters #FNname and #School in your stored procedure and use the above query in it.

Compare strings in SQL

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%'

SQL Wildcards using %%%

I'm wondering what problems I may not foresee by using this query:
SELECT *
FROM tanswer
WHERE CourseID LIKE '%%%' AND Q39 LIKE 'p' AND Q42 LIKE 'a' AND Q43 LIKE 'a'
In particular using '%%%'.
The reason being, I have a dropdown that would send a '%' and in the sql I enclose it with the other two % to search through a set of courses, this query leaving the courseID searching through all courses.
The recordset returned seems to work fine, but I'm not sure if there's anything I may not be seeing by using three %%% together. Is this a special use of wildcards that I've stumbled across?
These are equivalent:
CourseID LIKE '%%%'
CourseID LIKE '%%'
CourseID LIKE '%'
Which is why your query behaves as you expected it to.
You need to use ! to escape the inner %: i.e. %!%%
see here

Wildcards in sql

How does wildcards works in sql. If I do select * from table it give all the fields. But if I do select a* from table it gives error. Shouldn't it give all fields which begins with a?
I am little confused.
SELECT * FROM tableName literally means "select all columns from tableName".
Philip Graham is right about his answer where he asked to use a.*
Wildcards help you search for strings about which you are not sure. These are almost always used with the LIKE keyword and put in WHERE clauses or searched CASE statements.
There are two wildcard characters - % and _.
% is used to find any string of 0 or more length.
E.g.,
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J%'
This will return all the firstName from the persons table where firstname starts with letter J. This would return "Jason", "James", "Josh", "Jessica" and much more.
Note that UPPER function was used to eliminate case sensitivity.
Next, you can have an _ character that looks for the presence of one single character.
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J_M__'
This would return "James", "Jimmy", "Jamos", "Jxmx" and filter away any "Jason", "Jaguar", etc.
For more info click here
You can use a.* where a is the name of the table. For instance in
select a.* from a left join b on a.id = b.id
You would return only the fields from a but not from b
If want to use a wild card in SQL, You need to key on the column that you want to filter using LIKE.
SELECT *
FROM table
WHERE column_name LIKE 'a%';
This will give you everything that begins with 'a' on that column.
If you don't want all the columns, you must explicitly give the name of each column that you want in the query.
SELECT LastName, FirstName, Address
FROM table
So if you want all the fields that begin with 'a' you must name all the fields that begin with 'a' in the SELECT statement.
Hope this helps.

Oracle: Mixing IN and LIKE, is it possible? [duplicate]

This question already has answers here:
How can I introduce multiple conditions in LIKE operator?
(10 answers)
Closed 8 years ago.
Is it possible to do someting like this?
Ie.
IN ('%1', '%2')
The values are not few
How would you solve this task?
You could also use REGEXP_LIKE instead of LIKE. It can be more flexible in this type of situation.
To get everything that ends with '1' or '2' just use:
SELECT * FROM table_name WHERE REGEXP_LIKE(col_name, '[12]$')
The [12] means match 1 or 2. The $ means match the end of the string.
If you need something such as this:
LIKE '%1' OR LIKE '%2' or LIKE '%A' or LIKE '%W'
... you just have to add the other characters within the square brackets:
SELECT * FROM table_name WHERE REGEXP_LIKE(col_name, '[12AW]$')
Or, if you wanted anything that ends with a digit you could use this:
SELECT * FROM table_name WHERE REGEXP_LIKE(col_name, '[[:digit:]]$'
If you have only a few, just combine them with or:
column like '%1' or column like '%2' or...
If you have a lot, you can create a table with the patterns and join to it.
create table matches (
match char(10)
);
insert into matches values
('%1'),('%2'),...;
select * from table
inner join matches on table.column like matches.match;
That has some issues with duplicate rows if a single row matches more than one pattern, but you can modify it depending on what kind of final output you want.
As a third option, you could use substr() rather than like if they are all the same length:
select * from table where substr(column,-1,1) in ('1','2',...)
This checks if the last character is in the set of values.
How about using like itself?
Column LIKE '%1' OR Column LIKE '%2'
Basically same as examples of others but closest to your example:
SELECT deptno, empno, ename FROM scott.emp
WHERE job IN
(SELECT job FROM scott.emp WHERE job Like ('SALE%') OR job Like ('MANAG%'))
/