Structured Query Language [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Please explain some of these SQL code snippets to me:
select S.sid
from Student S
I don't know what the word "S" means after table name Student. Please give me a hand and explain.

S is an alias. Student S.
If you didn't specify an Alias, you could use:
select sid from Student
or select Student.sid from Student
The alias can be pretty much anything. E.G:
select aliasnameishere232fsdf.sid from Student aliasnameishere232fsdf
Alias's not only make query's easier to type, but can be useful for self-joins, for differentiation:
select S1.firstName, S2.firstName
from Student S1
JOIN Student S2 ON S2.someId = S1.someId

S is an alias. It's a shorter name given to Student in this query to make it easier to address.
It isn't very helpful in this particular query, but in longer and more complex queries, this technique really helps writing manageable code.

Related

SQL group by - can it be this simple? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
can someone please check if this is correct?
Not sure if my answer to Q6 is correct, I am not sure if the group_by I am using is right or not, the rest I think is ok
Thanks
You are close. You need to:
Use COUNT() instead of SUM().
There's no need to use the HAVING clause.
Optionally, you can add aliases to the columns, so they become easier to read.
Your query should look like:
select a.author_id, count(*) as titles, sum(b.quantity_ordered) as units
from a join b on a.book_id = b.book_id
group by a.author_id

Database table columns names [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have noticed that some people in database columns instead of for example id, user, addressare using something like h_id, e_user, f_address etc...
Is that some kind of security aspect? or maybe these are some shortcuts of words?
Its because there might be many id fields like user_id,category_id,that's why they use so that code is understandable.
And talking about columns name like f_address, they are just shortcut for say first address. It doesn't have anything to do with security but to increase the query readability use proper name to fields so that people can understand just by seeing column name what data it saves.
If there are fields like category_id and sub_category_id , it is understandable from the field name, but if i denote it using c_id and s_id, its hard to depict.
Well, 'User' is a security object in SQL Server, so using that is kind of scary. 'ID' and 'address' are way too generic to provide any semantics when used as attribute names.
If a purpose of design is to be maintainable and readable, then there some words that simply don't work.
Definitely not security related.
Some use it for readability or speed (you don't have to remember which table you gave a certain name->see following example) when writing queries.
i.e.
select a.name, b.name from table1 a join table2 b on a.id=b.id
Like this you have to remember that table1 is named a and table2 is named b etc.
But if you use tablename_field (which you can shorten by using only the first letter of the tablename). That way you never have duplicate fieldnames when creating join queries.

Sql databases select command [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am new to databases. And our teacher gave us pretty hard assignment. There are two tables. First table nickname is abilities(of superhero's:) ) and second table name superheros.
We have to select nick of Superhero and his average(medial) range for those who has two abilities?
Image of both tables:
Original here: http://postimg.org/image/85pqbc47n/
I will not give you solution - after all, it's homework and you have to learn something :) But I can give you an advice - try to do one task at a time
first, find those superheroes who has only 2 abilities (actually, you can do this by quering only table with abilities)
second - try to find average range of abilities for all superheroes (here you'll need join)
combine your queries
take a look at join, group by, count and having
Don't feel bad if you can't write it at first attempt, your query is not super easy, but 'm sure you can do this.
You can use HAVING and AVG() for this:
SELECT s.NickName, AVG(a.Range)
FROM abilities a
JOIN superhero s
ON a.ID_SuperHero = s.ID_SuperHero
GROUP BY s.NickName
HAVING COUNT(DISTINCT a.Abilities > 1)

Retrieve right year in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Goal:
"In which years was the Physics prize awarded but no Chemistry prize. (WARNING - this question is way too hard for this level, you will need to use sub queries or joins). "
Problem:
Don't know how to solve it.
The code can be found in "http://sqlzoo.net/1b.htm", headline 3a.
SELECT
distinct yr
FROM
nobel
WHERE
yr not in (select yr from nobel where subject in ('Chemistry')) AND
subject in ('Physics')
One option using subqueries. The subquery gets all the years where there was a chemistry prize which is used to to excude those years with the not in statement.
The distinct yr accounts for the fact that there could have been two prizes awarded.
There's a bunch of different ways to make this work. Take the code above and try making a join with the subquery work. That'll give you more practice with subqueries and using them in joins which is a useful thing.

Why does WordPress prefix its column names with the table name? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
I noticed that in a recent update WordPress decided to change all of its column names to include the table name prefix. WHY?!
The wp_posts table now has a column called post_title, for example. It used to just be title. I don't understand the reason for this change. There must've been one, yes?
I just don't understand what possible reason there could be since in SQL you can refer to things like table.column. Also, it must've been really difficult to change all of the code.
Perhaps to prevent confusion when making joins with tables having similar column names.
There is no good reason for doing this, plus it makes maintenance much more difficult.
There is no confusion if columns are specified like:
SELECT p.title, s.title FROM wp_posts p JOIN site s ON (p.site_id = s.id);