I can't apply multiple conditions in this SQL statement - sql-server

I don't know why it doesn't give me the answer for students enrolled in Database Systems but not in Operating System Design.
select student.snum, student.sname, enrolled.cname
-> from enrolled
-> inner join student ON enrolled.snum = student.snum
-> where enrolled.cname="Database Systems" AND enrolled.cname<>"Operating System Design";`
+-----------+--------------------+------------------+
| snum | sname | cname |
+-----------+--------------------+------------------+
| 112348546 | Joseph Thompson | Database Systems |
| 115987938 | Christopher Garcia | Database Systems |
| 348121549 | Paul Hall | Database Systems |
| 322654189 | Lisa Walker | Database Systems |
| 552455318 | Ana Lopez | Database Systems |
+-----------+--------------------+------------------+
My student table.
+-----------+--------------------+------------------------+-------+------+
| snum | sname | major | level | age |
+-----------+--------------------+------------------------+-------+------+
| 51135593 | Maria White | English | SR | 21 |
| 60839453 | Charles Harris | Architecture | SR | 22 |
| 99354543 | Susan Martin | Law | JR | 20 |
| 112348546 | Joseph Thompson | Computer Science | SO | 19 |
| 115987938 | Christopher Garcia | Computer Science | JR | 20 |
| 132977562 | Angela Martinez | History | SR | 20 |
| 269734834 | Thomas Robinson | Psychology | SO | 18 |
| 280158572 | Margaret Clark | Animal Science | FR | 18 |
| 301221823 | Juan Rodriguez | Psychology | JR | 20 |
| 318548912 | Dorthy Lewis | Finance | FR | 18 |
| 320874981 | Daniel Lee | Electrical Engineering | FR | 17 |
| 322654189 | Lisa Walker | Computer Science | SO | 17 |
| 348121549 | Paul Hall | Computer Science | JR | 18 |
| 351565322 | Nancy Allen | Accounting | JR | 19 |
| 451519864 | Mark Young | Finance | FR | 18 |
| 455798411 | Luis Hernandez | Electrical Engineering | FR | 17 |
| 462156489 | Donald King | Mechanical Engineering | SO | 19 |
| 550156548 | George Wright | Education | SR | 21 |
| 552455318 | Ana Lopez | Computer Engineering | SR | 19 |
| 556784565 | Kenneth Hill | Civil Engineering | SR | 21 |
| 567354612 | Karen Scott | Computer Engineering | FR | 18 |
| 573284895 | Steven Green | Kinesiology | SO | 19 |
| 574489456 | Betty Adams | Economics | JR | 20 |
| 578875478 | Edward Baker | Veterinary Medicine | SR | 21 |
+-----------+--------------------+------------------------+-------+------+
My enrolled table
+-----------+----------------------------+
| snum | cname |
+-----------+----------------------------+
| 112348546 | Database Systems |
| 115987938 | Database Systems |
| 348121549 | Database Systems |
| 322654189 | Database Systems |
| 552455318 | Database Systems |
| 455798411 | Operating System Design |
| 552455318 | Operating System Design |
| 567354612 | Operating System Design |
| 112348546 | Operating System Design |
| 115987938 | Operating System Design |
| 322654189 | Operating System Design |
| 567354612 | Data Structures |
| 552455318 | Communication Networks |
| 455798411 | Optical Electronics |
| 301221823 | Perception |
| 301221823 | Social Cognition |
| 301221823 | American Political Parties |
| 556784565 | Air Quality Engineering |
| 99354543 | Patent Law |
| 574489456 | Urban Economics |
+-----------+----------------------------+

You need to use the NOT EXISTS as follows:
select s.snum, s.sname, e.cname
from enrolled e
inner join student s ON e.snum = s.snum
where e.cname='Database Systems'
AND not exists
(select 1 from enrolled ee
where ee.snum = e.snum and e.cname = 'Operating System Design');

Compare string with single quotation mark(' ') than double quotation(" "). Your code seems ok to me.
Remember:
Single quotes are for strings.
Double quotes are for tables names and column names.
select student.snum, student.sname, enrolled.cname
from enrolled
inner join student ON enrolled.snum = student.snum
where enrolled.cname='Database Systems'
AND enrolled.cname<>'Operating System Design';
or try this
select student.snum, student.sname, enrolled.cname
from enrolled
inner join student ON enrolled.snum = student.snum
where enrolled.cname='Database Systems'

Related

Slicer to include 2 columns at once

So I have a view like this in SQL Server which I'm using to build a dashboard in Power BI:
ID | Name | IsRegional | IsFederal | Department | ...
1 | John | Yes | No | Paris | ...
2 | Mike | No | Yes | Brussels | ...
3 | Bill | No | Yes | Berlin | ...
4 | Bart | Yes | Yes | Berlin | ...
5 | Suzy | Yes | No | New York | ...
Currently I have 2 slicers in PowerBi that say "Is Regional: Yes/no" and "Is Federal: Yes/no". I want to make one slicer of this saying "Type: Federal/Regional"
My idea was to add a column TYPE in the view that says
WHEN IsRegional = 'Yes' THEN 'Regional'
WHEN IsFederal = 'Yes' THEN 'Federal'
ELSE 'None'
and then use the new column for the slicer
ID | Name | IsRegional | IsFederal | Type | Department | ...
1 | John | Yes | No | Regional | Paris | ...
2 | Mike | No | Yes | Federal | Brussels | ...
3 | Bill | No | Yes | Federal | Berlin | ...
4 | Bart | Yes | Yes | Regional | Berlin | ...
5 | Suzy | Yes | No | Regional | New York | ...
However, this creates an issue with record 4 where the type can be both. I would like the slicer to include row 4 when I have Federal selected as type (since it's both federal as Regional). Is there a way to solve this issue so I can use the single slicer? I would rather not add a 4th option saying "Both" to the slicer because I'm sure people will look over that one.
Just change your CASE expression slightly:
CASE WHEN IsRegional = 'Yes' AND IsFederal = 'Yes' THEN 'Both'
WHEN IsRegional = 'Yes' THEN 'Region'
WHEN IsFederal = 'Yes' THEN 'Federal'
ELSE 'None'
END

Designing a database for categories and subcategories

Basically I'm trying to figure out how Amazon architected their book section. Check out Amazon's book page here (https://www.amazon.com/s/ref=lp_2_ex_n_1?rh=n%3A283155&bbn=283155&ie=UTF8&qid=1522817105).
We are given several main categories: Arts & Photography, Biographies & Memoirs, etc.
If I click on Biographies & Memoirs for example, I'm lead to a series of sub categories. I.E. Biographies & Memoirs > Historical > Asia > Japan
There are repeating sub-category names for example: History > Asia > Japan
How can I map this kind of information so that it is scalable?
Below is the wrong way to do it...?
Categories table
+----+-----------------------+-----------+
| id | name | parent_id |
+----+-----------------------+-----------+
| 1 | Biographies & Memoirs | null |
| 2 | Historical | 1 |
| 3 | Asia | 2 |
| 4 | History | null |
| 5 | Asia | 4 |
| 6 | Japan | 5 |
| 7 | Japan | 3 |
+----+-----------------------+-----------+
Books
+----+-------------------------------------+----------+
| id | name | category |
+----+-------------------------------------+----------+
| 1 | The Lone Samurai | 7 |
| 2 | The Human Tradition in Modern Japan | 7 |
| 3 | Okinawa: The Last Battle | 6 |
+----+-------------------------------------+----------+
Authors
+----+---------------+----------+
| id | firstname | lastname |
+----+---------------+----------+
| 1 | James M. | Burns |
| 2 | Roy E. | Appleman |
| 3 | Russell A. | Gugeler |
| 4 | John | Stevens |
| 5 | William Scott | Wilson |
| 6 | Anne | Walthall |
+----+---------------+----------+
Authors to books (Many to many)
+---------+-----------+
| book_id | author_id |
+---------+-----------+
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 1 | 5 |
| 2 | 6 |
+---------+-----------+

How to select multiple rows with same foreignKey, but one column has all values in a set

So I'm currently working on a stored procedure that returns a table comprising of data about the user (user ID) and performance metrics (field, metric_obtained). I was originally doing it so I would return all data back, but I was thinking it would be more efficient to return only people who meet the minimum to be recognized. I've already filtered them out based on minimum requirement, but the thing is they can be qualified based on a combination of things, so if I have 3 metrics A, B, and C, one recognition can be for A and B, or another is just C. I'm also not limited to a max of 3 or this wouldn't be a problem.
My tables look like this:
|Employee | Metric | Obtained|
|_________|________|_________|
| John | Email | .98 |
| Sue | Email | .99 |
| Sue | Phone | .82 |
| Larry | Email | .93 |
| Larry | Phone | .83 |
| Jess | Phone | .9 |
| Jess | Email | .94 |
| Bob | Phone | .99 |
So if I need to get back both Phone AND Email my results would look like this:
|Employee | Metric | Obtained|
|_________|________|_________|
| Sue | Email | .99 |
| Sue | Phone | .82 |
| Larry | Email | .93 |
| Larry | Phone | .83 |
| Jess | Phone | .9 |
| Jess | Email | .94 |
Like I said, this would be easy if I had a guaranteed number of metrics, but I don't. Any thoughts?

SQL Database Constraint | Multi-table Constraint

I need to make 2 database constraints that connect two different tables at one time.
1. The total score of the four quarters equals the total score of the game the quarters belong to.
2. The total point of all the players equals to the score of the game of that team.
Here is what my tables look like.
quarter table
+------+--------+--------+--------+
| gNum | Period | hScore | aScore |
+------+--------+--------+--------+
| 1 | 1 | 13 | 18 |
| 1 | 2 | 12 | 19 |
| 1 | 3 | 23 | 31 |
| 1 | 4 | 32 | 18 |
| | | Total | Total |
| | | 80 | 86 |
+------+--------+--------+--------+
Game Table
+-----+--------+--------+--------+
| gID | hScore | lScore | tScore |
+-----+--------+--------+--------+
| 1 | 86 | 80 | 166 |
+-----+--------+--------+--------+
Player Table
+-----+------+--------+--------+
| pID | gNum | Period | Points |
+-----+------+--------+--------+
| 1 | 1 | 1 | 20 |
| | | 2 | 20 |
| | | 3 | 20 |
| | | 4 | 20 |
+-----+------+--------+--------+
So Virtually I need to use CHECK I think to make sure that players points = score of their team ie (hScore, aScore) and also make sure that the hScore and aScore = the total score in the Game table.
I was thinking of creating a foreign key variable on one of the tables and setting up constraints on that would this be the best way of going about it?
Thanks

Database design for download presets

Newbie with databases, I would like some advise please..
I have agencies who can download photo's.
Standard each agency can download "medium" & "large" photos.
Now from their account page I would like them to make extra custom presets and manage those.
I looked in the database of some blog software how they handle categories and wrapped my head around this example. Is this the right approach?
Cheers
agency 1 has preset "medium" & "large"
agency 2 has preset "medium", "large" & "Bill custom"
-----------
| presets |
-----------------------------------------------
| preset_id | preset_name | preset_dimensions |
-----------------------------------------------
| 1 | medium | 800x600 |
| 2 | large | 3000x2000 |
| 3 | Bill custom | 640x420 |
-----------------------------------------------
----------------
| preset_assoc |
------------------------------------------------------------
| presassoc_id | presassoc_preset_id | presassoc_agency_id |
------------------------------------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
| 4 | 2 | 2 |
| 5 | 3 | 2 |
------------------------------------------------------------
------------
| agencies |
---------------------------
| agency_id | agency_name |
---------------------------
| 1 | Joe ltd |
| 2 | Bill inc |
---------------------------
The approach is right. Because you have NxN relation (1 agency can have multiple presets, and the same preset could be used by multiple agencies) you need to have a joining table. The only questionable thing is that preset_assoc doesn't have to have presassoc_id because the other 2 columns could be used as a combined primary key.

Resources