Oracle query alphabet replacement [duplicate] - sql

This question already has answers here:
ORA 00904 Error:Invalid Identifier
(2 answers)
ORA-00984: column not allowed here [duplicate]
(1 answer)
Closed 17 days ago.
I want to print the FIRST_NAME from Intern table after replacing ‘a’ with ‘A’.
The table is as follows:
CREATE TABLE INTERN
(
INTERN_ID NUMBER,
FIRST_NAME VARCHAR2 (20),
LAST_NAME VARCHAR2 (20),
SALARY NUMBER,
JOINING_DATE DATE,
DEPARTMENT VARCHAR2 (20)
);
INSERT INTO INTERN VALUES (001, 'Monika', 'Arora', 100000, TO_DATE('2014-02-20 09:00:00','YYYY-MM-DD HH24:MI:SS'), 'HR');
INSERT INTO INTERN VALUES (002, 'Niharika', 'Verma', 80000, TO_DATE('2014-06-11 09:00:00','YYYY-MM-DD HH24:MI:SS'), 'Admin');
INSERT INTO INTERN VALUES (003, 'Vishal', 'Singhal', 300000, TO_DATE('2014-02-20 09:00:00','YYYY-MM-DD HH24:MI:SS'), 'HR');
INSERT INTO INTERN VALUES (004, 'Amitabh', 'Singh', 500000, TO_DATE('2014-02-20 09:00:00','YYYY-MM-DD HH24:MI:SS'), 'Admin');
INSERT INTO INTERN VALUES (005, 'Vivek', 'Bhati', 500000, TO_DATE('2014-06-11 09:00:00','YYYY-MM-DD HH24:MI:SS'), 'Admin');
INSERT INTO INTERN VALUES (006, 'Vipul', 'Diwan', 200000, TO_DATE('2014-06-11 09:00:00','YYYY-MM-DD HH24:MI:SS'), 'Account');
INSERT INTO INTERN VALUES (007, 'Satish', 'Kumar', 75000, TO_DATE('2014-01-20 09:00:00','YYYY-MM-DD HH24:MI:SS'), 'Account');
INSERT INTO INTERN VALUES (008, 'Geetika', 'Chauhan', 90000, TO_DATE('2014-04-11 09:00:00','YYYY-MM-DD HH24:MI:SS'), 'Admin');
I tried with this query:
SELECT REPLACE (FIRST_NAME, "a", "A") FROM INTERN;
getting "%s: invalid identifier" with this query.
Requesting help.
Thanks!

Have you tried changing " to ' ?
It is look like
SELECT REPLACE (FIRST_NAME, 'a', 'A') FROM INTERN;

Related

To find passengers travelling more than thrice on the same route in PL/SQL

Here I am creating three tables, one for storing the Train Info. Another for holding onto the Passenger info and the other one to hold the ticket Info.
create table T_Train_Info(
route_no integer primary key,
source varchar2(50),
destination varchar2(50)
)
create table T_Pay_Info(
pax_id integer primary key,
pax_name varchar2(50),
dob date,
gender varchar2(5)
)
create table T_Tkt_Info(
pax_id integer,
route_no integer,
journey_date date,
seat_no varchar2(5),
primary key(pax_id, route_no, journey_date)
)
In the Train_Info table, I am inserting two unique routes with the same source and destination as there can be different routes for the same source and destination. And filling the other tables in the same manner. In the ticket table, I am repeating values because I aim to find the passenger travelling thrice on the same route.
insert into T_Train_Info values(1, 'Chennai', 'Pune');
insert into T_Train_Info values(2, 'Chennai', 'Pune');
insert into T_Train_Info values(3, 'Bangalore', 'Kolkata');
insert into T_Tkt_Info values(100, 1, to_date('11/03/2022', 'DD/MM/YYYY'), 22);
insert into T_Tkt_Info values(100, 1, to_date('14/08/2022', 'DD/MM/YYYY'), 23);
insert into T_Tkt_Info values(100, 1, to_date('29/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(102, 3, to_date('22/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(100, 1, to_date('27/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(100, 2, to_date('28/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Pay_Info values(100, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(101, 'B', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');
insert into T_Pay_Info values(102, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(103, 'D', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');
insert into T_Pay_Info values(104, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(105, 'A', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');
Here's my procedure which keeps returning the error saying 'exact fetch returns more than requested number of rows' at the select statement. What am I doing wrong here?
create or replace procedure pr_pass_route_details(x in T_Train_Info.Source%type, y in T_Train_Info.Destination%type) is
pr_name T_Pay_Info.Pax_Name%type;
begin
for i in (select pax_id from t_tkt_info group by pax_id,route_no having count(*) >=3) loop
select pax_name into pr_name from t_pay_info where pax_id = i.pax_id and T_Train_Info.Source=x and T_Train_Info.Destination=y;
dbms_output.put_line(pr_name);
end loop;
end pr_pass_route_details;
i’m not sure why you’ve written a SP to do this as you can achieve this with a simple query:
SELECT
pax_id,
route_no,
COUNT(journey_date)
FROM T_Tkt_Info
GROUP BY
pax_id,
route_no
HAVING COUNT(journey_date) = 3

Oracle SQL Join columns on 2 conditions

I tried to search forums for my scenario but could not find anything remotely similar. So here goes my long winded explanation : I have 3 tables - order_fact , session_fact and orderline.
create table order_fact (order_no varchar2(20), order_timestamp date, cookie_id number, session_id number);
insert into order_fact values ('69857-20210329', to_date('29-MAR-2021 10:11:58', 'DD-MON-YYYY HH24:MI:SS'), 827678, 79853421);
insert into order_fact values ('78345-20210411', to_date('11-APR-2021 18:37:07', 'DD-MON-YYYY HH24:MI:SS'), 569834, 84886798);
insert into order_fact values ('79678-20210519', to_date('19-MAY-2021 20:51:34', 'DD-MON-YYYY HH24:MI:SS'), 589623, 89556782);
insert into order_fact values ('78759-20210411', to_date('11-APR-2021 09:46:52', 'DD-MON-YYYY HH24:MI:SS'), 685213, 77549823);
create table session_fact (cookie_id number, session_id number, session_timestamp date, marketing_vendor varchar2(30) , referral_type VARCHAR2(2) );
insert into session_fact values (827678, 79853421, to_date('29-MAR-2021 09:47:36', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (827678, 79853378, to_date('28-MAR-2021 12:47:36', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (827678, 79853313, to_date('24-MAR-2021 13:23:36', 'DD-MON-YYYY HH24:MI:SS'), 'Naaptol', 'S');
insert into session_fact values (827678, 79853254, to_date('23-MAR-2021 14:39:56', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (569834, 84886798, to_date('11-APR-2021 14:41:44', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (569834, 84886735, to_date('10-APR-2021 11:03:44', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (569834, 84886687, to_date('08-APR-2021 17:26:49', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (569834, 84886659, to_date('03-APR-2021 11:03:44', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (569834, 84886497, to_date('01-APR-2021 07:59:08', 'DD-MON-YYYY HH24:MI:SS'), 'Google', 'R');
insert into session_fact values (685213, 77549823, to_date('11-APR-2021 09:07:34', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (685213, 77549786, to_date('09-APR-2021 20:51:34', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (685213, 77549589, to_date('07-APR-2021 14:11:57', 'DD-MON-YYYY HH24:MI:SS'), 'FabShopping', 'D');
insert into session_fact values (685213, 77548356, to_date('03-APR-2021 15:38:42', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (589623, 89556782, to_date('19-MAY-2021 16:46:52', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (589623, 89556512, to_date('18-MAY-2021 09:46:52', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (589623, 89556477, to_date('13-MAY-2021 18:34:29', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
insert into session_fact values (589623, 89556348, to_date('10-MAY-2021 16:13:49', 'DD-MON-YYYY HH24:MI:SS'), '-1', 'D');
create table orderline (order_no varchar2(20), ol_nbr number, ol_ref varchar2(5));
insert into orderline values ('78345-20210411', 0, '-2');
insert into orderline values ('78345-20210411', 1, 'HV3');
insert into orderline values ('78345-20210411', 2, 'HV3');
insert into orderline values ('78759-20210411', 0, '-2');
insert into orderline values ('78759-20210411', 1, 'PS5');
insert into orderline values ('78759-20210411', 2, 'PS5');
insert into orderline values ('78759-20210411', 3, 'PS5');
insert into orderline values ('79678-20210519', 0, '-2');
insert into orderline values ('79678-20210519', 1, 'NPT');
insert into orderline values ('79678-20210519', 2, 'NPT');
insert into orderline values ('69857-20210329', 0, '-2');
insert into orderline values ('69857-20210329', 1, 'HV3');
insert into orderline values ('69857-20210329', 2, 'HV3');
insert into orderline values ('69857-20210329', 3, 'HV3');
As can be seen from above order_fact and session_fact tables are connected by cookie and session id. The request is to get these columns : ORDER_NO, MARKETING_VENDOR, REFERRAL_TYPE, OL_REF from the above 3 tables.
I have written the JOIN query :
select a.ORDER_NO, b.MARKETING_VENDOR,
b.REFERRAL_TYPE, c.OL_REF
FROM order_fact a
INNER JOIN session_fact b
ON (a.cookie_id = b.COOKIE_ID AND
b.session_timestamp < a.order_timestamp AND
b.session_timestamp > a.order_timestamp-7)
INNER JOIN orderline c ON
(a.ORDER_NO = c.ORDER_NO AND c.OL_NBR = 1);
Here is the sticky situation for me :
Get the data in session_fact table for a cookie_id in order_fact for timestamp of not more than 7 days before the order_timestamp. For example - order_no 78345-20210411 was placed on 11-APR-2021 18:37:07. Using the cookie id of that order I get all rows in session_fact till 11-APR - 7 days = 4-APR. So 3rd and 1st Apr data cannot be considered. This has been taken care in my query. But I wanted to mention why I had the additional AND clauses in the 1st JOIN ON condition.
From the data got in point 1 above do not consider those records where REFERRAL_TYPE = 'D' and MARKETING_VENDOR = '-1'. 'S' and '-1' can be considered and so is 'R' and '-1'. Basically any values can be considered as long as its NOT 'D' and '-1'. And select the record whose timestamp is closest to the order_timestamp in table order_fact. Now this is where it gets tricky - if there are no records of past 7 days where combo of REFERRAL_TYPE and MARKETING_VENDOR is NOT 'D' and '-1' then join the tables order_fact and session_fact on both cookie_id and session_id and fetch the values.
Join tables order_fact and orderline ON ORDER_NO and OL_NBR = 1. This also has been taken care in my join query.
So my only problem is getting the JOIN between session_fact and order_fact on the 2 different conditions mentioned in point 2. Can this be done by SQL? The Tech Lead of my team asked me to write a PL/SQL block. I did that because the original request was to add MARKETING_VENDOR, REFERRAL_TYPE, OL_REF columns in order_fact table and get the values from their respective tables. I cannot help but feel this can be done by SQL using CASE. Or am I wrong? If anyone could please help me with this query I will be grateful.
Edit : Adding the result data set
Edit : Any kind soul to help me out? 🙂 I take it it's not possible in a SQL statement.
And select the record whose timestamp is closest to the order_timestamp in table order_fact
From your description looks like you just need Top 1 record by session_timestamp:
with
step1 as (
SELECT
a.ORDER_NO
,a.order_timestamp
,c.MARKETING_VENDOR
,c.REFERRAL_TYPE
,c.session_timestamp
FROM order_fact a
cross apply (
select *
from session_fact b
where a.cookie_id = b.COOKIE_ID
and (REFERRAL_TYPE,MARKETING_VENDOR) not in (('D','-1'))
AND b.session_timestamp < a.order_timestamp
--AND b.session_timestamp > a.order_timestamp-7
order by b.session_timestamp desc
fetch first 1 rows only
) c
)
select
s.*
,o.OL_REF
FROM
step1 s
JOIN orderline o
ON (s.ORDER_NO = o.ORDER_NO AND o.OL_NBR = 1)
;
Result:
ORDER_NO ORDER_TIMESTAMP MARKETING_VENDOR REFERRAL_TYPE SESSION_TIMESTAMP OL_REF
-------------- ------------------- ---------------- ------------- ------------------- ------
78345-20210411 2021-04-11 18:37:07 Google R 2021-04-01 07:59:08 HV3
78759-20210411 2021-04-11 09:46:52 FabShopping D 2021-04-07 14:11:57 PS5
69857-20210329 2021-03-29 10:11:58 Naaptol S 2021-03-24 13:23:36 HV3

Foreign Key Constraint Failed - Even when I try to manually add data

I am studying SQL in my data science degree program, and the assignment is to perform SQL queries for the first time. In order to do that, I have to paste the SQL starter code in and run it. However, when I do, I hit an error that I can't solve. My class is online and my professor hasn't responded and, since this isn't part of the graded part of the assignment, I figured I'd try here to get help on this error.
Edit: It was pointed out that I didn't include all of the tables. Please see full code for all tables and all data. I was trying to avoid flooding this post with a lot of lines of code, but it seems I left out pertinent information. See below.
The tables were created with these blocks of code:
create table section (
course_id varchar(8),
sec_id varchar(8),
semester varchar(6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
year numeric(4,0) check (year >1701 and year < 2100),
building varchar(15),
room_number varchar(7),
time_slot_id varchar(4),
primary key (course_id, sec_id, semester, year),
foreign key (course_id) references course on delete cascade,
foreign key (time_slot_id) references timeslot on delete no action,
foreign key (building, room_number) references classroom on delete set null
);
create table teaches (
ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id, sec_id, semester, year) references section on delete cascade,
foreign key (ID) references instructor on delete set null
);
create table instructor (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2) check (salary >10000),
primary key (ID),
foreign key (dept_name) references department on delete set null
);
Then, I added data to instructor and section with the follow. Side note, I had to go in and manually set the time_slot_id as a primary key to avoid getting an error.
INSERT INTO classroom VALUES ('Innovation Hall', '101', 10);
INSERT INTO classroom VALUES ('Innovation Hall', '327', 30);
INSERT INTO classroom VALUES ('Exploratory Hall', '202', 20);
INSERT INTO classroom VALUES ('Research Hall', '302', 20);
INSERT INTO classroom VALUES ('Arts Center', '212B', 100);
INSERT INTO classroom VALUES ('Research Hall', '310', 30);
INSERT INTO classroom VALUES ('Innovation Hall', '121', 10);
INSERT INTO classroom VALUES ('Innovation Hall', '321', 20);
INSERT INTO classroom VALUES ('Arts Center', '120A', 100);
INSERT INTO classroom VALUES ('Exploratory Hall', '231', 20);
INSERT INTO classroom VALUES ('Innovation Hall', '220', 30);
INSERT INTO department VALUES ('CDS', 'Research Hall', 100000);
INSERT INTO department VALUES ('Music', 'Arts Center', 70000);
INSERT INTO department VALUES ('History', 'Arts Center', 50000);
INSERT INTO department VALUES ('Geography', 'Exploratory Hall', 60000);
INSERT INTO department VALUES ('Biology', 'Exploratory Hall', 85000);
INSERT INTO department VALUES ('Physics', 'Innovation Hall', 90000);
INSERT INTO department VALUES ('Math', 'Exploratory Hall', 55000);
INSERT INTO course VALUES ('CDS-101', 'Intro to CDS', 'CDS', 3);
INSERT INTO course VALUES ('CDS-130', 'Computing for Scientists', 'CDS', 3);
INSERT INTO course VALUES ('CDS-302', 'Databases', 'CDS', 3);
INSERT INTO course VALUES ('CDS-303', 'Data Mining', 'CDS', 3);
INSERT INTO course VALUES ('MUS-100', 'Fundamentals of Music', 'Music', 3);
INSERT INTO course VALUES ('HIS-101', 'History of Western Civilization', 'History', 3);
INSERT INTO course VALUES ('GGS-101', 'Major World Regions', 'Geography', 4);
INSERT INTO course VALUES ('BIO-101', 'Intro to Biology', 'Biology', 3);
INSERT INTO course VALUES ('BIO-301', 'Genetics', 'Biology', 4);
INSERT INTO course VALUES ('PHY-101', 'Intro to Physics', 'Physics', 3);
INSERT INTO course VALUES ('PHY-403', 'Quantum Mechanics', 'Physics', 4);
INSERT INTO course VALUES ('MAT-110', 'Probabilities', 'Math', 3);
INSERT INTO course VALUES ('MAT-114', 'Calculus', 'Math', 4);
INSERT INTO prereq VALUES ('CDS-130', 'CDS-101');
INSERT INTO prereq VALUES ('MAT-114', 'MAT-110');
INSERT INTO prereq VALUES ('PHY-403', 'PHY-101');
INSERT INTO prereq VALUES ('CDS-302', 'CDS-130');
INSERT INTO prereq VALUES ('CDS-302', 'PHY-403');
INSERT INTO prereq VALUES ('CDS-303', 'CDS-302');
INSERT INTO prereq VALUES ('CDS-303', 'MAT-114');
INSERT INTO instructor VALUES ('11111', 'Turing', 'CDS', 950000);
INSERT INTO instructor VALUES ('11112', 'Widom', 'CDS', 100000);
INSERT INTO instructor VALUES ('12121', 'Mozart', 'Music', 75000);
INSERT INTO instructor VALUES ('22222', 'Heraclitus', 'History', 65000);
INSERT INTO instructor VALUES ('12345', 'Eratosthenes', 'Geography', 55000);
INSERT INTO instructor VALUES ('54321', 'Crick', 'Biology', 88000);
INSERT INTO instructor VALUES ('33333', 'Newton', 'Physics', 80000);
INSERT INTO instructor VALUES ('44444', 'Euler', 'Math', 77000);
INSERT INTO instructor VALUES ('55555', 'Euclid', 'Math', 77000);
INSERT INTO timeslot VALUES ('A', 'M', 8, 0 , 9, 15);
INSERT INTO timeslot VALUES ('B', 'M', 10, 0 , 11, 15);
INSERT INTO timeslot VALUES ('C', 'T', 8, 0 , 9, 15);
INSERT INTO timeslot VALUES ('D', 'T', 10, 0 , 11, 15);
INSERT INTO timeslot VALUES ('E', 'T', 11, 15 , 12, 30);
INSERT INTO timeslot VALUES ('F', 'W', 9, 0 , 10, 15);
INSERT INTO timeslot VALUES ('G', 'R', 8, 0 , 10, 15);
INSERT INTO timeslot VALUES ('H', 'R', 15, 0 , 16, 15);
INSERT INTO timeslot VALUES ('I', 'F', 9, 0 , 10, 15);
INSERT INTO section VALUES ('CDS-101', '1', 'Fall', 2019, 'Research Hall', '310', 'A');
INSERT INTO section VALUES ('CDS-130', '1', 'Fall', 2019, 'Innovation Hall', '121', 'B');
INSERT INTO section VALUES ('CDS-302', '1', 'Fall', 2019, 'Innovation Hall', '327', 'C');
INSERT INTO section VALUES ('CDS-302', '2', 'Spring', 2019, 'Innovation Hall', '321', 'C');
INSERT INTO section VALUES ('MUS-100', '1', 'Fall', 2019, 'Arts Center', '120A', 'A');
INSERT INTO section VALUES ('HIS-101', '1', 'Fall', 2019, 'Arts Center', '212B', 'B');
INSERT INTO section VALUES ('GGS-101', '1', 'Fall', 2019, 'Exploratory Hall', '202', 'D');
INSERT INTO section VALUES ('BIO-101', '2', 'Fall', 2019, 'Exploratory Hall', '231', 'E');
INSERT INTO section VALUES ('PHY-403', '1', 'Spring', 2019, 'Innovation Hall', '220', 'F');
INSERT INTO section VALUES ('BIO-101', '2', 'Fall', 2020, 'Exploratory Hall', '231', 'C');
INSERT INTO section VALUES ('PHY-403', '1', 'Spring', 2020, 'Innovation Hall', '220', 'D');
I didn't have a problem. Finally, I tried to run this:
INSERT INTO teaches VALUES ('11111', 'CDS-101', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('11112', 'CDS-130', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('12121', 'MUS-100', '1', 'Fall', 2019);
INSERT INTO teaches VALUES ('54321', 'BIO-101', '2', 'Fall', 2019);
INSERT INTO teaches VALUES ('33333', 'PHY-403', '1', 'Spring', 2019);
INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);
The error occurred on the last line. I always get:
Result: FOREIGN KEY constraint failed
At line 1: (Note: I tried re-running it by itself afterwards, thus the "Line 1")
INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);
I checked over each constraint, looked at each column, and couldn't figure it out. I even went in and entered the data manually in the "Browse Data" tab within DB Browser Lite; while I was able to enter the first 4 values, when I try to enter anything for year, I get the same error in a popup box. Even when I click in the box, leave it blank, then try to click out, I get the same error. See image.
Please assist... I'm lost at this point and quite frustrated.
There are two things here:
the issue, and
how to work out what the issue is.
Let's start with 2).
The error message is:
Result: FOREIGN KEY constraint failed
At line 1:
INSERT INTO teaches VALUES ('44444', 'MAT-114', '2', 'Fall', 2019);
That tells us to look at the foreign keys defined on the teaches table.
There are 2:
ID references the ID in instructor table (aside: maybe call this instructor_id instead of ID, it'll make more sense in 6 weeks when you come back to look at it).
course_id, sec_id, semester, year reference the section table.
There is, in your data, an instructor with ID '44444', so that's probably not the problem.
But it doesn't look like there's a row in section with 'MAT-114', '2', 'Fall', 2019 and that is likely your problem.
Note also that there's not one for 'MTH-114' like in the image, either.

SQL query for courses, semester and students

Hi I have a schema that looks like this
I was trying to make these 3 queries
Find the names of the top 4 instructors who have taught the most number of distinct courses. Display also the total number of courses taught.
Output columns: InstructorName, NumberOfCoursesTaught
Sort by: NumberOfCoursesTaught in descending order
Find the top 3 semesters in which the most number of courses were offered. (Treat Spring of 2009 and Spring of 2010 as two different semesters.
Output columns: Semester, Year, NumberOfCourses
Sort by: NumberOfCourses in descending order
Find the top 2 students who have taken the most number of courses.
Output columns: S_ID, StudentName, NumberOfCourses
Sort by: NumberOfCourses in descending order
For query 1 I wrote
Select name AS InstructorName, count(course_id) AS NumberOfCourses
from Teaches where name IN
(SELECT name FROM Instructor where Instructor.i_id = Teaches.i_id)
group by i_id
order by count(course_id) DESC;
For query 2
SELECT semester, year, count(course_id) as
NumberOfCourses from Takes WHERE year='2009'
group by semester, year
order by count(course_id) DESC;
For query 3
SELECT s_id as S_ID, name as StudentName, count(course_id) as NumberOfCourses
FROM Takes where name IN
(SELECT name from Student where Takes.s_id = Student.s_id)
group by s_id
order by count(course_id) DESC;
Query 1 and 3 give the error
ORA-00904: "NAME": invalid identifier
Query 2 is giving an output, but it's wrong. I need help making the 3 queries correct
Test Data is
tables file is
create table classroom (building varchar(15), room_number varchar(7), capacity numeric(4,0), primary key (building, room_number));
create table department (dept_name varchar(20), building varchar(15), budget numeric(12,2) check (budget > 0), primary key (dept_name));
create table course (course_id varchar(8), title varchar(50), dept_name varchar(20), credits numeric(2,0) check (credits > 0),
primary key(course_id));
create table instructor (i_ID varchar(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2) check (salary > 29000), primary key (i_ID));
create table section (course_id varchar(8), sec_id varchar(8), semester varchar(6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')), year numeric(4,0) check (year > 1701 and year < 2100), building varchar(15), room_number varchar(7), time_slot_id varchar(4), primary key (course_id, sec_id, semester, year));
create table teaches (i_ID varchar(5), course_id varchar(8), sec_id varchar(8), semester varchar(6), year numeric(4,0), primary key (i_ID, course_id, sec_id, semester, year));
create table student (s_ID varchar(5), name varchar(20) not null, dept_name varchar(20), tot_cred numeric(3,0) check (tot_cred >= 0), primary key (s_ID));
create table takes (s_ID varchar(5), course_id varchar(8), sec_id varchar(8), semester varchar(6), year numeric(4,0), grade varchar(2), primary key (s_ID, course_id, sec_id, semester, year));
create table advisor (s_ID varchar(5), i_ID varchar(5), primary key (s_ID));
create table time_slot (time_slot_id varchar(4), day varchar(1),start_hr numeric(2) check (start_hr >= 0 and start_hr < 24), start_min numeric(2) check (start_min >= 0 and start_min < 60), end_hr numeric(2) check (end_hr >= 0 and end_hr < 24), end_min numeric(2) check(end_min >= 0 and end_min < 60), primary key (time_slot_id, day, start_hr, start_min));
create table prereq (course_id varchar(8), prereq_id varchar(8), primary key (course_id, prereq_id));
create table grade_points(grade varchar(2), points Number(10,4), primary key (grade));
data file is
delete from prereq;
delete from time_slot;
delete from advisor;
delete from takes;
delete from student;
delete from teaches;
delete from section;
delete from instructor;
delete from course;
delete from department;
delete from classroom;
-- Classroom
insert into classroom values ('Packard', '101', '500');
insert into classroom values ('Painter', '514', '10');
insert into classroom values ('Taylor', '3128', '70');
insert into classroom values ('Watson', '100', '30');
insert into classroom values ('Watson', '120', '50');
-- Department
insert into department values ('Biology', 'Watson', '90000');
insert into department values ('Comp. Sci.', 'Taylor', '100000');
insert into department values ('Elec. Eng.', 'Taylor', '85000');
insert into department values ('Finance', 'Painter', '120000');
insert into department values ('History', 'Painter', '50000');
insert into department values ('Music', 'Packard', '80000');
insert into department values ('Physics', 'Watson', '70000');
-- Course
insert into course values ('BIO-101', 'Intro. to Biology', 'Biology', '4');
insert into course values ('BIO-301', 'Genetics', 'Biology', '4');
insert into course values ('BIO-399', 'Computational Biology', 'Biology', '3');
insert into course values ('CS-101', 'Intro. to Computer Science', 'Comp. Sci.', '4');
insert into course values ('CS-190', 'Game Design', 'Comp. Sci.', '4');
insert into course values ('CS-315', 'Robotics', 'Comp. Sci.', '3');
insert into course values ('CS-319', 'Image Processing', 'Comp. Sci.', '3');
insert into course values ('CS-347', 'Database System Concepts', 'Comp. Sci.', '3');
insert into course values ('EE-181', 'Intro. to Digital Systems', 'Elec. Eng.', '3');
insert into course values ('FIN-201', 'Investment Banking', 'Finance', '3');
insert into course values ('HIS-351', 'World History', 'History', '3');
insert into course values ('MU-199', 'Music Video Production', 'Music', '3');
insert into course values ('PHY-101', 'Physical Principles', 'Physics', '4');
-- Instructor
insert into instructor values ('10101', 'Srinivasan', 'Comp. Sci.', '65000');
insert into instructor values ('12121', 'Wu', 'Finance', '90000');
insert into instructor values ('15151', 'Mozart', 'Music', '40000');
insert into instructor values ('22222', 'Einstein', 'Physics', '95000');
insert into instructor values ('32343', 'El Said', 'History', '60000');
insert into instructor values ('33456', 'Gold', 'Physics', '87000');
insert into instructor values ('45565', 'Katz', 'Comp. Sci.', '75000');
insert into instructor values ('58583', 'Califieri', 'History', '62000');
insert into instructor values ('76543', 'Singh', 'Finance', '80000');
insert into instructor values ('76766', 'Crick', 'Biology', '72000');
insert into instructor values ('83821', 'Brandt', 'Comp. Sci.', '92000');
insert into instructor values ('98345', 'Kim', 'Elec. Eng.', '80000');
-- Section
insert into section values ('BIO-101', '1', 'Summer', '2009', 'Painter', '514', 'B');
insert into section values ('BIO-301', '1', 'Summer', '2010', 'Painter', '514', 'A');
insert into section values ('CS-101', '1', 'Fall', '2009', 'Packard', '101', 'H');
insert into section values ('CS-101', '1', 'Spring', '2010', 'Packard', '101', 'F');
insert into section values ('CS-190', '1', 'Spring', '2009', 'Taylor', '3128', 'E');
insert into section values ('CS-190', '2', 'Spring', '2009', 'Taylor', '3128', 'A');
insert into section values ('CS-315', '1', 'Spring', '2010', 'Watson', '120', 'D');
insert into section values ('CS-319', '1', 'Spring', '2010', 'Watson', '100', 'B');
insert into section values ('CS-319', '2', 'Spring', '2010', 'Taylor', '3128', 'C');
insert into section values ('CS-347', '1', 'Fall', '2009', 'Taylor', '3128', 'A');
insert into section values ('EE-181', '1', 'Spring', '2009', 'Taylor', '3128', 'C');
insert into section values ('FIN-201', '1', 'Spring', '2010', 'Packard', '101', 'B');
insert into section values ('HIS-351', '1', 'Spring', '2010', 'Painter', '514', 'C');
insert into section values ('MU-199', '1', 'Spring', '2010', 'Packard', '101', 'D');
insert into section values ('PHY-101', '1', 'Fall', '2009', 'Watson', '100', 'A');
-- Teaches
insert into teaches values ('10101', 'CS-101', '1', 'Fall', '2009');
insert into teaches values ('10101', 'CS-315', '1', 'Spring', '2010');
insert into teaches values ('10101', 'CS-347', '1', 'Fall', '2009');
insert into teaches values ('12121', 'FIN-201', '1', 'Spring', '2010');
insert into teaches values ('15151', 'MU-199', '1', 'Spring', '2010');
insert into teaches values ('22222', 'PHY-101', '1', 'Fall', '2009');
insert into teaches values ('32343', 'HIS-351', '1', 'Spring', '2010');
insert into teaches values ('45565', 'CS-101', '1', 'Spring', '2010');
insert into teaches values ('45565', 'CS-319', '1', 'Spring', '2010');
insert into teaches values ('76766', 'BIO-101', '1', 'Summer', '2009');
insert into teaches values ('76766', 'BIO-301', '1', 'Summer', '2010');
insert into teaches values ('83821', 'CS-190', '1', 'Spring', '2009');
insert into teaches values ('83821', 'CS-190', '2', 'Spring', '2009');
insert into teaches values ('83821', 'CS-319', '2', 'Spring', '2010');
insert into teaches values ('98345', 'EE-181', '1', 'Spring', '2009');
-- Student
insert into student values ('00128', 'Zhang', 'Comp. Sci.', '102');
insert into student values ('12345', 'Shankar', 'Comp. Sci.', '32');
insert into student values ('19991', 'Brandt', 'History', '80');
insert into student values ('23121', 'Chavez', 'Finance', '110');
insert into student values ('44553', 'Peltier', 'Physics', '56');
insert into student values ('45678', 'Levy', 'Physics', '46');
insert into student values ('54321', 'Williams', 'Comp. Sci.', '54');
insert into student values ('55739', 'Sanchez', 'Music', '38');
insert into student values ('70557', 'Snow', 'Physics', '0');
insert into student values ('76543', 'Brown', 'Comp. Sci.', '58');
insert into student values ('76653', 'Aoi', 'Elec. Eng.', '60');
insert into student values ('98765', 'Bourikas', 'Elec. Eng.', '98');
insert into student values ('98988', 'Tanaka', 'Biology', '120');
-- Takes
insert into takes values ('00128', 'CS-101', '1', 'Fall', '2009', 'A');
insert into takes values ('00128', 'CS-347', '1', 'Fall', '2009', 'A-');
insert into takes values ('12345', 'CS-101', '1', 'Fall', '2009', 'C');
insert into takes values ('12345', 'CS-190', '2', 'Spring', '2009', 'A');
insert into takes values ('12345', 'CS-315', '1', 'Spring', '2010', 'A');
insert into takes values ('12345', 'CS-347', '1', 'Fall', '2009', 'A');
insert into takes values ('19991', 'HIS-351', '1', 'Spring', '2010', 'B');
insert into takes values ('23121', 'FIN-201', '1', 'Spring', '2010', 'C+');
insert into takes values ('44553', 'PHY-101', '1', 'Fall', '2009', 'B-');
insert into takes values ('45678', 'CS-101', '1', 'Fall', '2009', 'F');
insert into takes values ('45678', 'CS-101', '1', 'Spring', '2010', 'B+');
insert into takes values ('45678', 'CS-319', '1', 'Spring', '2010', 'B');
insert into takes values ('54321', 'CS-101', '1', 'Fall', '2009', 'A-');
insert into takes values ('54321', 'CS-190', '2', 'Spring', '2009', 'B+');
insert into takes values ('55739', 'MU-199', '1', 'Spring', '2010', 'A-');
insert into takes values ('76543', 'CS-101', '1', 'Fall', '2009', 'A');
insert into takes values ('76543', 'CS-319', '2', 'Spring', '2010', 'A');
insert into takes values ('76653', 'EE-181', '1', 'Spring', '2009', 'C');
insert into takes values ('98765', 'CS-101', '1', 'Fall', '2009', 'C-');
insert into takes values ('98765', 'CS-315', '1', 'Spring', '2010', 'B');
insert into takes values ('98988', 'BIO-101', '1', 'Summer', '2009', 'A');
insert into takes values ('98988', 'BIO-301', '1', 'Summer', '2010', null);
-- Advisor
insert into advisor values ('00128', '45565');
insert into advisor values ('12345', '10101');
insert into advisor values ('23121', '76543');
insert into advisor values ('44553', '22222');
insert into advisor values ('45678', '22222');
insert into advisor values ('76543', '45565');
insert into advisor values ('76653', '98345');
insert into advisor values ('98765', '98345');
insert into advisor values ('98988', '76766');
-- Time_slot
insert into time_slot values ('A', 'M', '8', '0', '8', '50');
insert into time_slot values ('A', 'W', '8', '0', '8', '50');
insert into time_slot values ('A', 'F', '8', '0', '8', '50');
insert into time_slot values ('B', 'M', '9', '0', '9', '50');
insert into time_slot values ('B', 'W', '9', '0', '9', '50');
insert into time_slot values ('B', 'F', '9', '0', '9', '50');
insert into time_slot values ('C', 'M', '11', '0', '11', '50');
insert into time_slot values ('C', 'W', '11', '0', '11', '50');
insert into time_slot values ('C', 'F', '11', '0', '11', '50');
insert into time_slot values ('D', 'M', '13', '0', '13', '50');
insert into time_slot values ('D', 'W', '13', '0', '13', '50');
insert into time_slot values ('D', 'F', '13', '0', '13', '50');
insert into time_slot values ('E', 'T', '10', '30', '11', '45 ');
insert into time_slot values ('E', 'R', '10', '30', '11', '45 ');
insert into time_slot values ('F', 'T', '14', '30', '15', '45 ');
insert into time_slot values ('F', 'R', '14', '30', '15', '45 ');
insert into time_slot values ('G', 'M', '16', '0', '16', '50');
insert into time_slot values ('G', 'W', '16', '0', '16', '50');
insert into time_slot values ('G', 'F', '16', '0', '16', '50');
insert into time_slot values ('H', 'W', '10', '0', '12', '30');
-- Prereq
insert into prereq values ('BIO-301', 'BIO-101');
insert into prereq values ('BIO-399', 'BIO-101');
insert into prereq values ('CS-190', 'CS-101');
insert into prereq values ('CS-315', 'CS-101');
insert into prereq values ('CS-319', 'CS-101');
insert into prereq values ('CS-347', 'CS-101');
insert into prereq values ('EE-181', 'PHY-101');
-- Grade_points
insert into grade_points values ('A+', 4.0);
insert into grade_points values ('A', 4.0);
insert into grade_points values ('A-', 3.7);
insert into grade_points values ('B+', 3.3);
insert into grade_points values ('B', 3.0);
insert into grade_points values ('B-', 2.7);
insert into grade_points values ('C+', 2.3);
insert into grade_points values ('C', 2.0);
insert into grade_points values ('C-', 1.7);
insert into grade_points values ('D+', 1.3);
insert into grade_points values ('D', 1.0);
insert into grade_points values ('D-', 0.7);
insert into grade_points values ('F', 0.0);
insert into grade_points values ('NP', 0.0);
insert into grade_points values ('U', 0.0);
Expected Query 1:
Srinivasan 3
Brandt 2
Crick 2
Katz 2
My result of Query 2:
Fall 2009 9
Spring 2009 3
Summer 2009 1
Expected Query 2:
Spring 2010 7
Spring 2009 3
Fall 2009 3
Expected Query 3:
12345 Shankar 4
45678 Levy 3
I cant give you the full solution because you still learning. But here are some guide lines.
First one:
Select name AS InstructorName, count(course_id) AS NumberOfCourses
from Teaches where name IN ....
Teaches doesn't have name, instead of WHERE you need JOIN to Instructor table
Second one:
You dont need filter WHERE year='2009' what they ask is you GROUP BY year, semester if you do GROUP BY semester then all Spring semester will be on the same group
Third one: Same as first. You need JOIN to STUDENTS

How can I format the output of my SQL query to display nicely?

I've defined my own database to play around and learn SQL (using SQL*Plus via SSH to remote into my school's linux machines). However, I've been having problems displaying my tables nicely, specifically this one:
CREATE TABLE customer_account
(
ACCOUNT_ID NUMBER(10) NOT NULL,
PHONE_NUMBER VARCHAR(20) NOT NULL,
EMAIL VARCHAR(100) NOT NULL,
FNAME VARCHAR(100) NOT NULL,
LNAME VARCHAR(100) NOT NULL,
ADDRESS_STREET VARCHAR(50) NOT NULL,
ADDRESS_CITY VARCHAR(20) NOT NULL,
ADDRESS_STATE VARCHAR(2) NOT NULL,
ADDRESS_ZIP VARCHAR(5) NOT NULL,
BIRTH DATE DEFAULT NULL,
PRIMARY KEY (ACCOUNT_ID)
);
INSERT INTO customer_account
VALUES (1, '9174560091', 'jhunters01#cuny.edu', 'Jack', 'Hunter', '11 67ST', 'New York', 'NY', '10024', TO_DATE('1998/01/22 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (2, '7134560012', 'L.Larson#gmail.com', 'Linda', 'Larson', '100-9 Brooklyn Hwy', 'New York', 'NY', '11225', TO_DATE('1996/12/20 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (3, '5303056927', 'sciencerules#gmail.com', 'Albert', 'Newton', '1206 Francis Mine', 'Sacramento', 'CA', '95814', TO_DATE('2001/05/17 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (4, '5106204676', 'luvlucy#yahoo.com', 'Ricky', 'Ricardo', '90 maple street west', 'Trenton', 'NJ', '08861', TO_DATE('1942/12/01 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (5, '3237843058', 'RalphJRiggins#dayrep.com', 'Ralph', 'Riggins', '3373 Hillhaven Drive', 'Los Angeles', 'CA', '90017', TO_DATE('1964/10/02 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (6, '2133384287', 'lavonnaRWilliams#mail.com', 'Lavonna', 'Williams', '1305 Zimmerman Lane', 'City of Commerce', 'CA', '90040', TO_DATE('1983/03/03 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (7, '6313604478', 'antoninetteRe#gmail.com', 'Antoinette', 'Reynolds', '2329 Wayback Lane', 'Smithtown', 'NY', '11787', TO_DATE('1990/10/25 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (8, '9736948587', 'Mcdonald#yahoo.com', 'Berger', 'McDonald', '3024 Spring Haven Trail', 'Mountain View', 'NJ', '07470', TO_DATE('1960/06/17 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (9, '9082074677', 'M.Lester#gmail.com', 'Moe', 'Lester', '2980 Williams Mine Road', 'Lakewood', 'NJ', '08701', TO_DATE('1988/10/05 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (10, '8282351937', 'son#rhyta.com', 'Dam', 'Son', '98 McVaney Road', 'Canton', 'NC', '28716', TO_DATE('1957/08/28 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
Whenever I did
SQL> SELECT * FROM customer_account;
the entire table does not come out nicely no matter what I tried. I've used set linesize to no avail. This is the best I could do
Is there too much going on per column in the actual table or could I do something to fix this?
I recommend using SQL developer.
Here are 2 suggestions.
Oracle SQL Developer
http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index-097090.html
SQuirreL SQL
http://squirrel-sql.sourceforge.net/