I'm trying to do an assignment for my course where one of the questions is to get the faculty name, date hired (in dd-mon-yyyy format) and that have the same title as Erit Jackson in the database, who's title is Assoc Prof. My code looks identical to the example provided by the instructor, but when I run this code, I get the error:
2014 - Commands out of sync; you can't run this command now
I don't know what I'm doing wrong thus far?
SELECT CONCAT('Dr.', ' ', LName) As 'Faculty Name', DateHired
FROM Faculty
WHERE Title = (
SELECT Title
FROM Faculty
WHERE Title = 'Assoc Prof');
This doesn't make a lot of sense as written. It can be simplified:
SELECT CONCAT('Dr.', ' ', f1.LName) As 'Faculty Name', f1.DateHired
FROM Faculty f1
INNER JOIN Faculty f2
ON f1.Title = f2.Title
WHERE f2.FName = 'Erit'
AND f2.LName = 'Jackson';
The way you are using the subquery is redundant. You're asking the database for the person that has the title(select & from), that matches(where title =) a table of titles with the title of 'Assoc Prof'(subquery).
Your professor wants your subquery to find the title of Erit Jackson, not to specifically find the title "Assoc Prof". Do you see the difference? You won't ever have the text "Assoc Prof" in your query at all.
Related
def fetchProposalByStudio(studioId: Int): List[ProposalDetails] = {
ConnectionPoolManager.getDB(config = appConfig).localTx { implicit session: DBSession =>
logger.info("Querying proposal table to fetch all the proposals")
SQL("""SELECT [except name] p.id, id, proposal_title, challenge, possible_solution, explanation,
| submission_date, status, submitted_by, remark
| FROM proposal p inner join knolder k on k.id = p.knolder_id
| where k.studio_id =? order by p.id desc""".stripMargin)
.bind(studioId)
.map(rs =>
ProposalDetails(
rs.int("id"),
rs.int("id"),
rs.string("proposal_title"),
rs.string("challenge"),
rs.string("possible_solution"),
rs.string("explanation"),
rs.string("submission_date"),
Some(ProposalStatus.withName(rs.string("status"))),
rs.string("submitted_by"),
rs.string("remark"),
**rs.string("name")**
)
)
.list().apply()
}
}
I don't want to fetch this column name in my query but without involving this in the query i am getting this error due to using case class.
13:28:24.446 [default-akka.actor.default-dispatcher-8] INFO c.k.l.b.ProposalImpl - Something went wrong while fetching the proposals. Exception message: ERROR: syntax error at or near "["
Position: 8
Smells of a syntax problem...
Perhaps:
SELECT [except name] -- should be
SELECT `except name` -- in mysql
If you don't want a particular column in an SQL resultset, you simply don't mention it in the SELECT.
There is no notion of SELECT * EXCEPT FirstName FROM person - if Person has FirstName, LastName, Age, Address and you don't want FirstName, you don't put it in the select list:
SELECT LastName, Age, Address FROM Person
^^^^^^^
no FirstName mentioned here
Mention every column you do want, do not mention any column you don't want.
If the complaint is "but there are 527 columns and I want all except one" - you can do something like:
SELECT CONCAT(column_name, ',') FROM information_schema.columns WHERE table_name = 'Person' and column_name <> 'FirstName'
which produces a resultset like:
LastName,
Age,
Address,
... 523 other columns
And you can then copy that resultset and paste it into your code, and it already has commas on the end..
If you want the columns all on one line, use GROUP_CONCAT or use a decent text editor to replace \r\n with nothing. If you want to surround the column name in backticks, put it into the CONCAT.. The ultimate point here is that you're a software developer: you can write code that writes code, then you can copy the output, which is valid code, and paste it into some other code somewhere else
BOOK_ADOPTION (course:int, sem:int, book_isbn:int)
TEXT (book_isbn:int, booktitle: varchar(50), publisher: varchar(50), author: varchar(50))
List the books which are adopted by the course as well as enrolled by the student.
I am trying this:
select course
from text
natural join book_adoption
group by course
having count(book_isbn)>1
and publisher = 'perason';
and getting an error:
Unknown column 'publisher' in 'having clause'
but the column publisher is there and even the spelling is correct.
Can someone help me out with this one?
HAVING clause is for using with aggregating functions (like count) but in the case of publisher you have to simply use the WHERE condition.
select course
from text natural join book_adoption
where publisher = 'perason'
group by course
having count(book_isbn)>1;
I am attempting to creat a VIEW for my class. I was instructed to make my Access Compatible syntax ANSI 92. I have done that but I keep getting a message saying to:
"Enter parameter Value for 'JR'."
But it should just be showing any with the JR in the field value.
Any idea what I am doing wrong?
SELECT Student.IDno, LastName, FirstName, Class AS
Junior, Major, GPA
FROM Student, Person
WHERE Student.IDno = Person.IDno
AND Class = “JR”;
I also have to include columns from 2-3 different tables, change the titles of two of the columns, and show only the records that have class = "JR".
Any advice on where to start looking for this info would be great.
Thank you.
EDIT: Thank you for the help with that last one. Now I'm trying to change this to be able to pull from multiple tables and I can't seem to figure anything out. When I run this code, it tells me it can't find the table or query. If I run each section alone, it works just fine. I need to pull some info from 4 tables...
SELECT Student.IDno, Person.LastName, Person.FirstName, Student.Class, Student.Major, Student.GPA
FROM Student, Person
WHERE Student.IDno = Person.IDno
AND Class = 'JR';
UNION
SELECT Student.IDno, Enrollment.OfferNo, Enrollment.Grade
FROM Student, Enrollment
WHERE Student.IDno = Enrollment.IDno;
The double-quotes present in your SQL code are 'smart quotes' or Unicode character 0x201C, as opposed to the standard double-quote, which is Unicode/ASCII character 0x0022.
Hence changing the SQL to either:
SELECT Student.IDno, LastName, FirstName, Class AS Junior, Major, GPA
FROM Student, Person
WHERE Student.IDno = Person.IDno
AND Class = "JR";
or:
SELECT Student.IDno, LastName, FirstName, Class AS Junior, Major, GPA
FROM Student, Person
WHERE Student.IDno = Person.IDno
AND Class = 'JR';
Will solve this.
However, I would also strongly suggest using an INNER JOIN (i.e. ANSI-92 syntax) in place of a cartesian product with join criteria in the WHERE clause (ANSI-89 syntax):
SELECT Student.IDno, LastName, FirstName, Class AS Junior, Major, GPA
FROM Student INNER JOIN Person ON Student.IDno = Person.IDno
WHERE Class = 'JR';
It is also advisable to state the table qualifier for each field (e.g. Person.LastName or Student.LastName) so as to avoid the improper use of reserved words or ambiguity arising from identically named fields in multiple tables.
In the code you have added to your question, you are attempting to UNION two SELECT queries which output a different number of columns, and presumably, different data types - this is invalid.
I imagine that you are actually looking to do this:
select
student.idno,
person.lastname,
person.firstname,
student.class,
student.major,
student.gpa,
enrollment.offerno,
enrollment.grade
from
(
student inner join person
on student.idno = person.idno
)
inner join enrollment
on student.idno = enrollment.idno
where
student.class = 'JR'
hi I'm just starting to learn sql and I wish to combine the columns into one using concat but didn't manage to do it. I can run the code without concat but when when I use concat it gives me an error code. Can anyone tell me what am I doing wrong?
SELECT CONCAT('A purchase with the purchase ID of' AS "Constraint",
ONLINEPURCHASE.PurchaseID AS "OLID", 'is an online purchase of type' AS "Condition", ONLINEPURCHASE.OnlineType AS "OLType", 'and also a walkin purchase of location' AS "Condition", WALKINPURCHASE.ShopLocation AS "ShopLocation")
FROM ONLINEPURCHASE JOIN WALKINPURCHASE
ON ONLINEPURCHASE.PurchaseID = WALKINPURCHASE.PurchaseID
WHERE WALKINPURCHASE.PurchaseID IN (SELECT PurchaseID
FROM WALKINPURCHASE);
But got this error (ERROR 1583 (42000): Incorrect parameters in the call to native function 'concat')
Congratulations on starting to learn SQL, it's super useful! Let me try to clear up your misunderstanding.
The CONCAT function takes some number of either strings or column values and joins them together. There's no need to use the AS keyword within it.
Here's what I think you're looking for:
SELECT
CONCAT(
'A purchase with the purchase ID of',
ONLINEPURCHASE.PurchaseID,
'is an online purchase of type',
ONLINEPURCHASE.OnlineType,
'and also a walkin purchase of location',
WALKINPURCHASE.ShopLocation
) AS result
FROM ONLINEPURCHASE JOIN WALKINPURCHASE
ON ONLINEPURCHASE.PurchaseID = WALKINPURCHASE.PurchaseID
WHERE WALKINPURCHASE.PurchaseID IN
(SELECT PurchaseID FROM WALKINPURCHASE);
This will create the sentence you're building in the CONCAT statement and return it as a single column of your result set. (I've used the AS keyword to rename the column you've built.)
Please let me know if you have any questions about this.
I'm studying for my Database System exam tomorrow, and I'm working on an SQL question. This question is the only one from the paper that doesn't have an answer, but here's the question:
We use the following schema:
Professor(name, office, dept, age) (age is key)
Course(cno, title, dept) (cno stands for course number and is a key, title is the name of the course and dept is name of department offering the course)
Enrollment(cno, semester, inst_name, enrollment) (the key for this is (cno, semester)
Question: Write the following sql query: Output a table containing the single row "yes" in it if the age difference between the oldest and the youngest professors teaching the Database Systems course between 2000 and 2009 is at most 5 years
I amn't sure my approach is right, since we don't exactly want to output something from the table. Note that I think enrollment corresponds to when the instructor started teaching the course (which isn't the usual definition AFAIK).
My approach is as follows:
WITH dbsProfs AS (
SELECT P.age
FROM Professor P, Enroll E, Course C
WHERE P.name = E.inst_name AND C.cno = E.cno AND C.title = "Database Systems"
AND E.enrollment BETWEEN 2000 and 2009
)
SELECT "Yes"
FROM dbsProfs
WHERE MAX(dbsProfs.age) - MIN(dbsProfs.age) <= 5
I'm fairly confident with my temporary table. I'm doing a join on all 3 tables and filtering out to include only the ones relevant to my query. It's the other half I'm unsure about.
Any insight on whether this is correct/how to correct this would be much appreciated. I amn't convinced WHERE MAX(dbsProfs.age) - MIN(dbsProfs.age) <= 5 is valid SQL
With aggregate function you shoud use having
SELECT "Yes"
FROM dbsProfs
HAVING MAX(dbsProfs.age) - MIN(dbsProfs.age) <= 5
WITH dbsProfs AS (
SELECT MIN(P.age) as min_age,MAX(P.age) as max_age
FROM Professor P, Enroll E, Course C
WHERE P.name = E.inst_name AND C.cno = E.cno AND C.title = "Database Systems"
AND E.enrollment BETWEEN 2000 and 2009
)
SELECT CASE WHEN min_age<max_age THEN "Yes" END
FROM dbsProfs
It's true, you can't put aggregate functions in your WHERE statement. This question will take a bit more work.
This is the approach I would take:
SELECT CASE WHEN A.MaxAge - A.MinAge <=5 THEN "Yes" END
FROM
(
SELECT Max(Professor.age) AS MaxAge, Min(Professor.age) AS MinAge
FROM Professor INNER JOIN Enroll ON Professor.name = Enroll.inst_name
INNER JOIN Course ON Enroll.cno = Course.cno
WHERE Course.title = "Database Systems" AND Enroll.enrollment BETWEEN 2000 AND 2009
) AS A
This assumes enrollment is indeed the year the course was instructed (as you also assumed). You can use an iif statement if you're fond of SQL Server instead of the SELECT CASE (the latter being more t-SQL standards friendly).